PIL 图像压缩

Ste*_*fan 1 python optimization image image-compression python-imaging-library

我正在编写一个脚本,用于调整和优化要在网站上使用的图像(在本例中为 Iphone 图片)。到目前为止,我编写了这个简单的脚本,它使文件大小减少了大约 35%,但我希望我可以进一步缩小文件大小。有没有办法进一步优化这个脚本?任何帮助深表感谢!

from PIL import Image

img = Image.open('file location')
newImage = img.resize((1024, 768), Image.ANTIALIAS)         

newImage.save('new file location', optimize=True, quality=95)

newImage.show()                                               
Run Code Online (Sandbox Code Playgroud)

geo*_*xsh 5

您可以使用pyguetzli尝试 Google 的 Guetzli 编码器,它通常生成较小的 jpeg 文件,但需要大量时间,比较:

  • 原版:9.4M
  • pil_1024*768_q95.jpeg:638K
  • pil_1024*768_q85.jpeg:404K
  • guetzli_1024*768_q95.jpg:376K

原始 jpeg 文件来自wiki common,作者为 Diego Delso,CC BY-SA 4.0。