How to know the compression level of a PNG file and whether it is lossy?

3

Can I know whether a PNG file was compressed and how (at least, whether it was a lossless compression) in Windows? When I open the standard file properties dialog from the Windows Explorer, I see no properties regarding this.

TecMan

Posted 2015-05-26T08:39:42.263

Reputation: 966

Answers

8

PNG is always compressed by DEFLATE algorithm, as mandated by PNG specification. This is same algorithm used by zip compressor, among others.

There are no lossy compression algorithms for PNGs. PNG is always loseless.

Disclaimer: There are methods of "optimizing" PNG size by decreasing quality (color depth) of an image before saving it as PNG. This has nothing to do with PNG itself and cannot be reliably detected neither in PNG nor the original file.

Agent_L

Posted 2015-05-26T08:39:42.263

Reputation: 1 493

Many image edit apps allow you to specify some additional parameters when saving your work as PNG. For instance, in paint.net we see the 'Dithering Level' option. As for the ZIP compression, we can also specify the compression level using such archivers as 7-Zip. What about that? Is there a way to read this info from the PNG header if it is possible at all? – TecMan – 2015-05-26T10:34:14.280

2@TecMan "Dithering Level" is an option of reducing the image to 8bit color, not saving PNG. Note that it's same for every 8-bit save, not only for PNGs. It's completely unrelated to file operation, showing it there is merely convenience. In other editors you do "convert->8bit" first and then "save as". As for choosing zip compression level, I haven't thought about that. But (as it's loseless) it doesn't affect the image, only affects memory usage of compressing and decompressing (in 7-zip you can see estimate). – Agent_L – 2015-05-26T10:46:33.127

If we talk about paint.net, "Dithering Level" and "Bit Depth" are two different options we can set while saving an image as PNG. And we can have different effects and images of different sizes playing with each option. – TecMan – 2015-05-26T15:30:41.427

1

@TecMan: When you set the compression level using 7-Zip or any other archiver, can it be retrieved from a ZIP file? Similarly it doesn't seem to be stored in the PNG header either. As for dithering, it has to do with reducing banding in the image. It's not directly related to PNG compression in any way. The point is, why do you want to know these details in the first place?

– Karan – 2015-05-26T18:26:47.283

@Karan, In some cases I need to know whether I'm looking at PNG screenshots with reduced quality or they reflect the screen picture exactly as it was. As I can see, the PNG format does not allow us to store this info and easily retrieve it. – TecMan – 2015-05-27T08:22:30.400

@TecMan Any quality reductions come before saving as PNG. There is no way you can retrieve info about entire history of processing of given image in any file format. – Agent_L – 2015-05-27T12:23:45.223

1@TecMan: I can't think of any instance where knowing the compression level would make a difference when looking at screenshots, but maybe it's important to you for some reason. Regardless, you have your answer. – Karan – 2015-05-27T13:50:17.970

2

Estimating compression ratio of any image file is actually pretty simple. You have to know the width, height and bit depth of the image. To calculate how much data would be needed by uncompressed raw image data you have to do this simple thing: raw data size = image width * image heigth * (bits per pixel / 8). Then just divide raw data size by your PNG's file size by and you have the estimated compression ratio (not exact value because of the headers, etc.). For example 640x480x32 image would need 640 * 480 * (32 / 8) which is 1 273 800 bytes. Now lets assume your PNG has 200kB. You divide (200 * 1024) / 1273800. That gives you a compression ratio of about 0.16.

And remember about one fact. Sometimes 24 bit images are actually stored as 32 bit value. So you have to take that into account.

pvc

Posted 2015-05-26T08:39:42.263

Reputation: 309

It is a manual and a bit rough calculation. The question was about an automatic way to know that. Anyway, +1 to your answer. – TecMan – 2015-05-26T10:28:00.820