在 Python 中创建 PNG 文件
我有一个应用程序,我希望能够从 Python 中的数据生成 PNG 图像。我做了一些搜索,发现“PIL”看起来相当过时。还有其他图书馆更适合这个吗?
I have an application where I would like to be able to generate PNG images from data in Python. I've done some searching and found "PIL" which looked pretty outdated. Is there some other library that would be better for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的 PNG 文件可以很容易地从纯 Python 代码生成 - 您所需要的只是标准 zlib 模块和一些字节编码来写入块。这是一个完整的示例,普通读者可以将其用作自己的 png 生成器的入门:
Simple PNG files can be generated quite easily from pure Python code - all you need is the standard zlib module and some bytes-encoding to write the chunks. Here is a complete example that the casual reader may use as a starter for their own png generator:
这是一个 Python3 示例:
Here's a Python3 example:
png 包将是一个合理的常见选择。
这是项目描述:
The png package would be a reasonable common choice.
Here's the project description:
经过一番搜索后,我找到了这个网站: https://www.daniweb.com/programming/software-development/code/454765/save-a-pygame-drawing。它只是使用旧的 pygame。
第 37 行:(
pg.image.save(win, fname)
) 将 pygame 表面上绘制的任何内容保存为文件。编辑
这是实际的代码:
After doing some searching, I found this site: https://www.daniweb.com/programming/software-development/code/454765/save-a-pygame-drawing. It just uses good old pygame.
Line 37: (
pg.image.save(win, fname)
) saves whatever is drawn on the pygame surface as a file.EDIT
Here is the actual code:
我知道这个问题是 9 年前提出的,但在 Python 3.8 时代,你可以以“写入二进制”模式打开文件:
I'm aware that this question was asked over 9 years ago, but in the age of Python 3.8, you can just open the file in "Writing Binary" mode: