4
votes

Hey, i'm using a program called pngquant to convert 24 bit PNGs to 8-bit PNGs. Everything seems to work fine, and I don't notice any loss of quality for icons and other images that don't contain too much colors. Now when I feed it a PNG photo with zillions of colours, it produces a PNG8 where I can see some quality loss.

I'd like to determine that quality loss programmatically. I'd like to know when converting a PNG24 to PNG8 is safe or not. Sort of what webpagetest.org does -- they tell you that this specific image will be smaller in size if converted to PNG8 and will not loose quality.

Any ideas?

Thanks.

3
Anything less than 256 pixels (i.e. 16x16) can be converted to 256 colors losslessly.Mark Ransom

3 Answers

3
votes

This sounds like a full-reference image quality assessment problem.

The simplest way to approach this is to try computing the PSNR between the PNG24 and PNG8 images. This is a measure of the difference between the two images. The higher the PSNR, the less different the images are. After using your color quantization software, check if the PSNR is above some threshold (you'll have to determine that empirically), and if it is, then the quantization was "safe".

PSNR has its down sides, namely the fact that it doesn't always correspond to the way the human visual system works (for example, it neglects the phenomenon of spatial and contrast masking). Another metric, SSIM, attempts to take care of that problem, but is slightly more difficult to compute (here is an OpenCV implementation, though). You can use SSIM instead of PSNR in the thresholding approach I described above.

Here's another thread which you might find useful.

2
votes

Quite simple. If the image you are converting from PNG24 to PNG8 has more thant 256 colors, you gonna loose quality. Do I missed something?

0
votes

For development of pngquant I use my own SSIM tool, since the OpenCV-based one didn't seem to support gamma correction nor alpha channel properly.

When you run pngquant -v it will output amount of error introduced as MSE=n (n is mean square error — 0 is perfect quality).

The latest version has --quality setting which lets you set minimum required quality. If it can't achieve it, it won't save the file.