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 the 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
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. According to the documentation for
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:
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
high. 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.
License CC BY-NC-ND 4.0