Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

python - Why is the quality of JPEG images produced by PIL so poor?

The JPEG images created with PIL (1.1.7) have very poor quality. Here is an example:

Input: https://s23.postimg.cc/8bks3x5p7/cover_1.jpg

Output: https://s23.postimg.cc/68ey9zva3/cover_2.jpg

The output image was created with the following code:

from PIL import Image
im = Image.open('/path/to/cover_1.jpg')
im.save('/path/to/cover_2.jpg', format='JPEG', quality=100)

The red text looks really awful. Saving the image with GIMP or Photoshop does not even come close to the bad quality created by PIL. Does somebody know why this happens and how it can be solved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two parts to JPEG quality. The first is the quality setting which you have already set to the highest possible value.

JPEG also uses chroma subsampling, assuming that color hue changes are less important than lightness changes and some information can be safely thrown away. Unfortunately in demanding applications this isn't always true, and you can most easily notice this on red edges. PIL doesn't expose a documented setting to control this aspect.

Edit by Pascal Beyeler:

I just found an option which disables subsampling. You can set subsampling=0 when saving an image and the image looks way sharper! Thanks for your Help Mark!

im.save('/path/to/cover-2.jpg', format='JPEG', subsampling=0, quality=100)

Edit once more:

The subsampling option is finally documented, but I don't know how long that's been the case. Looks like you have the option of either an integer argument or a string; I still recommend 0 as shown above.

https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...