JPEG quality 与图像质量

https://jdhao.github.io/2019/07/20/pil_jpeg_image_quality/

Introduction

The other day, I was haunted by a bug and found that it was an issue with the image quality saved by Pillow after spending a few hours debugging the code.

Initially, the workflow of my code was like loading the JPEG image with PIL and after some operation, saving the image again. But I didn’t give any other parameters except the file name when using Image.save() method.

The quality parameter

After looking up the documentation, I find that PIL will save the image with quality factor 75 by default so that the image quality is decreased. The documentation for quality says:

The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality.

Using a quality factor of 95 should be enough to preserve the image quality:

img.save('test.jpg', quality=95)

Subsampling

According to this post, even if you use quality=100, the saved image will still be slightly different from the original image. This is because PIL will also use subsampling technique to reduce the image size. You can use subsampling=0 to turn off this feature. Overall, to retain the content of original image, you may use:

img.save('test.jpg', quality=100, subsampling=0)

Other options for quality

For JPEG files, there are also a list of preset options you can use for the quality parameter, such web_highhigh. The complete list of options can be found here. These preset options will set the subsampling factor and quantization table which is used in image quantization process.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值