python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.17k stars 2.22k forks source link

Values above 95 should be avoided - but why? Image file formats - JPG #7541

Closed FurkanGozukara closed 11 months ago

FurkanGozukara commented 11 months ago

Here your documentation says this

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

quality The image quality, on a scale from 0 (worst) to 95 (best), or the string keep. 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. The value keep is only valid for JPEG files and will retain the original image quality level, subsampling, and qtables.

Why avoid 95+ any particular reason?

I am ok with speed and size when I set it 100

radarhere commented 11 months ago

https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/usage.txt

For most images, specifying a quality value above about 95 will increase the size of the compressed file dramatically, and while the quality gain from these higher quality values is measurable (using metrics such as PSNR or SSIM), it is rarely perceivable by human vision.

FurkanGozukara commented 11 months ago

So no other side effect?

radarhere commented 11 months ago

We might adjust the buffer size, but otherwise, this is actually a question about how libjpeg behaves.

Looking through https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/usage.txt,

-quality 100 will generate a quantization table of all 1's, minimizing loss in the quantization step (but there is still information loss in subsampling, as well as roundoff error.)

FurkanGozukara commented 11 months ago

can you tell me in simple terms this?

I am using this to train Stable Diffusion model

will 100% quality cause any issues compared to 95%?

radarhere commented 11 months ago

My first comment was saying that quality above 95 is unlikely to make an improved that is perceptible to humans.

Adjusting the buffer size shouldn't have any effect on the output, it is just an internal difference to allow the image to save correctly.

Really, if you are concerned about quality, I would suggest you save images as PNGs instead, since PNG is a lossless format, and JPEG is lossy - meaning that JPEGs may not save images with the exact same pixels that were in your image.

FurkanGozukara commented 11 months ago

I know and I am agree. but original images are JPEG so i am not sure if saving as PNG will bring any improvement

radarhere commented 11 months ago

Opening a JPEG and saving it as a PNG will not make the image better than the original, no. But opening a JPEG and saving it as a JPEG again can make it look worse than the original.

https://imagekit.io/blog/jpeg-image-degradation/

while JPEG has provided us with several benefits like reduced file sizes, faster transmission, storage efficiency, and the like, it has also introduced a new problem — JPEG degradation. This term describes the loss of image quality that occurs when a JPEG image is edited and/or re-saved

radarhere commented 11 months ago

@FurkanGozukara did you have any further questions?

FurkanGozukara commented 11 months ago

thank you