How to determine when PNG24 converted to PNG8 is lossless?

Hey, I'm using a program called pngquant to convert 24-bit PNGs to 8-bit PNGs. Everything seems to be working fine, and I don't notice any quality loss for icons and other images that don't contain too many colors. Now, when I feed him a PNG photograph with shades of flowers, he creates PNG8 where I see a loss of quality.

I would like to determine that quality loss is programmatic. I would like to know when the conversion of PNG24 to PNG8 is safe or not. It seems like what webpagetest.org does - they tell you that this particular image will be smaller if it is converted to PNG8 and does not lose quality.

Any ideas?

Thanks.

+4
source share
3 answers

This sounds like a problem with a full reference image quality rating .

The easiest way to get closer to this is to try to calculate the PSNR between PNG24 and PNG8 images. This is a measure of the difference between the two images. The higher the PSNR, the smaller the images. After using the color quantization software, check if the PSNR exceeds a certain threshold (you will have to determine it empirically), and if so, then the quantization was “safe”.

PSNR has its downsides, namely the fact that it does not always correspond to how the human visual system works (for example, it neglects the phenomenon of spatial and contrast masking). Another metric, SSIM , is trying to take care of this problem, but it is a little more difficult to calculate (here is the OpenCV implementation , though). You can use SSIM instead of PSNR in the threshold method described above.

Here is another thread that may come in handy.

+3
source

Pretty simple. If the image that you convert from PNG24 to PNG8 has more than 256 colors, you will lose quality. Did I miss something?

+2
source

I use my own SSIM tool to develop pngquant, since OpenCV-based seems to not support gamma correction or alpha channel properly.

When you run pngquant -v you will get the number of errors entered as MSE=n ( n - average square error - 0 - excellent quality).

The latest version has the --quality parameter, which allows you to set the minimum required quality. If he cannot achieve this, he will not save the file.

0
source

Source: https://habr.com/ru/post/1333784/


All Articles