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

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 didn’t originally expose a documented setting to control this aspect.

Pascal Beyeler discovered the option which disables chroma subsampling. You can set subsampling=0 when saving an image and the image looks way sharper!

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

The Pillow project took over where PIL left off and made many improvements, including documenting the formerly undocumented subsampling option. It’s been enhanced to take either an integer or string argument, but I still recommend 0 as shown above.

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

Note also that the documentation claims quality=95 is the best quality setting and that anything over 95 should be avoided. This may be a change from earlier versions of PIL.

Leave a Comment