Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 528
Pascal Avatar asked Oct 10 '13 18:10

Pascal


People also ask

Why is JPEG low quality?

Why does it happen? JPEG is a lossy compression format (read more about image compression in this article). Generally speaking, the algorithm behind this format makes some trade-offs to lower the image size. When done correctly, we don't see any noticeable differences between an uncompressed image and its JPEG version.

What is the best quality for JPEG?

As a general benchmark: 90% JPEG quality gives a very high-quality image while gaining a significant reduction on the original 100% file size. 80% JPEG quality gives a greater file size reduction with almost no loss in quality.

Does JPEG have good quality?

All JPEG Images Are High Resolution, Print-Quality Photos: False. Print quality is determined by the pixel dimensions of the image. An image must have at least 480 x 720 pixels for an average quality print of a 4" x 6" photo. It must have 960 x 1440 pixels or even more for a medium- to high-quality print.


1 Answers

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

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.

like image 189
Mark Ransom Avatar answered Oct 05 '22 08:10

Mark Ransom