python - 使用 savefig 时生成较小文件大小的 PNG 选项

标签 python matplotlib

我正在使用 matplotlib 生成绘图,然后使用 matplotlib.pyplot.savefig 将其保存到 PNG 文件中。

一切正常,但文件大小相当大(大约 120Kb)。

之后我可以使用 ImageMagik(通过 shell)通过减少颜色数量和关闭抖动将文件大小减小到 38Kb 而不会降低质量:

convert +dither -colors 256 orig.png new.png

我的问题是:我可以在 matplotlib 中执行此操作吗?我搜索了文档,但找不到任何与设置保存时使用的颜色数量等相关的内容....

谢谢!

最佳答案

这就是我通过 PIL(现在是 Pillow)运行 matplotlib 图像所做的

import cStringIO
import matplotlib.pyplot as plt
from PIL import Image

...

ram = cStringIO.StringIO()
plt.savefig(ram, format='png')
ram.seek(0)
im = Image.open(ram)
im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)
im2.save( filename , format='PNG')

关于python - 使用 savefig 时生成较小文件大小的 PNG 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10784652/

相关文章:

python - 如何从 Python 中的字符串中获取整数值?

python - 被屏蔽的 RGB 图像不会出现被 imshow 屏蔽

python - 如何在 python 上制作网格(使用 matplotlib 或其他库)

python - OS X 上的 Matplotlib.pyplot 和来自 Python.org 的 64 位 Python

python - 在Python中将字符串转换为二进制整数

python - 在python中转换时区的意外结果

python - 每当抛出警告时退出

python - 如何在 Django 中使用带有 Protocol Buffer 的 gRPC

latex - Matplotlib latex 工作目录/搜索路径

python - Matplotlib/Seaborn : how to plot a rugplot on the top edge of x-axis?