我正在使用 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/