How to Inflate a png compressed by zopfli

770 Views Asked by At

I compressed a png file using ImageOptim. I used the Zopfli as the method of compressing. ImageOptim guarantees that it's a lossless compression. Truly my png size has reduced, but how can i decompress it back to the original size?

I read about Zopfli compression, it said that it compresses png files using deflate algorithm. How can inflate/decompress the compressed png back to original file in java?

2

There are 2 best solutions below

6
On

Zopli does not optimize the already compressed stream of PNG data. It works on decompressed image from scratch. So you can not undo the process. If you insist on getting the exact same result before the optimization you can decompress to bitmap and encode to PNG using the original algorithm, again.

A little more explanation: Let's simplistically say you need to compress the string "1111111111". A first lossless compression function which has a max buffer size of 5 bytes would output it as "5x1,5x1"(5 times 1 then 5 times 1), then a second implementation of the same algorithm which has a max buffer of 10 bytes outputs "10x1" which looks better. Both results are of course lossless, but most often you can not go to "5x1,5x1" from "10x1".

To make sure the compressions are really lossless you may simply decompress and compare uncompressed versions. In your case, decompress both original PNG and optimized PNG to bitmap and compare bitmaps.

No need to program;

Use ImageMagick to compare files

Lossless Compression:

Command:

magick compare -metric mae anydesk00000.png anydesk00000.png diff.png`

Output:

0 (0)

A value of 0 shows zero difference, hence no loss.

Lossy Compression:

Command:

magick compare -metric mae anydesk00000.png anydesk00000.jpg diff.png

Output:

151.861 (0.00231724)
7
On

You don't need to decompress it, it should be a png file useable as is, unless you are doing something wrong. The image is not changed, only the binary representation in disk.

There is no way to go to the original file, you could recompress it but you would need to know the library used to create it as well as the compression level and other settings used to create it (filters, deflate strategy, sliding window size,... most png files are created with default values but not all). Usually there is no need, but if you need to keep the original file (e.g. to match a md5 checksum) you shouldn't use zopfli.

Edit: there are two common definitions of lossless compression:

  1. Being able to recreate a bit-to-bit exact copy of the input file.
  2. Being able to recover the input file contents without changes. Bit exact reproduction of the input file is not a goal.

General file compression uses the first definition. If you zip a .jpg file you can get back exactly the same .jpg file.

Domain specific lossless compression typically uses the second definition, if you create a PNG file from a JPEG you won't be able to recover the original JPEG file (even if you know that the original was a JPEG image), but the original JPEG and the PNG will have exactly the same image data, yet everybody calls PNG lossless.

If you consider a program that uses Zopfli to reduce PNG files size as a "PNG file compressor", it won't be lossless using the general definition, but it will be using the domain specific one. If the program claims to compress losslessly the image rather than the file there is no ambiguity, it's the second definition. If the program claims to be a lossless optimizer it's also the second definition, there is no ambiguity.