Can I encode a PNG image losslessly to another format and decode it back into an identical file?

I am archiving roughly 150gb of PNG assets. In an effort to increase the compression ratio of the images, I am trying out FLIF (Free Lossless Image Format). This provides an excellent compression ratio, and I am able to get all of the files down to around 80gb by using the following arguments:

flif -e --effort=100 input.png output.flif

For compatibility reasons (these assets are a part of a much larger project), I require the ability to then decode these images back into identical, byte-for-byte files. I am decoding like so:

flif -d out.flif out.png

However, the decoded PNG is not identical to the original, and I can confirm that EXIF data is not the cause. Rather, it seems that the color type/compression settings differ.

I am a software developer, and I think it would be a fun project to write a tool that could store some info about the original image and then re-compress the decoded image with its original settings, but that begs the question - is that even possible? I've been looking into ImageMagick, and that seems like it may be able to handle the task, but that begs the question:

Is it even possible to do this? That is, re-compress a PNG with the proper settings such that it will be identical to the original, byte-for-byte? Or will the binary contents of the PNG file always be arbitrary depending on what was used to encode it in the first place?

tl;dr:

I want to take two pixel-identical PNG images (A and B) encoded with different settings and be able to re-compress B such that it will be bytewise-identical to A.


  1. PNG, itself, is lossless, but has varying levels of compression. The compression algorithm trades time for space, so it is possible that saving a PNG file in more compressed format would save some space, though in the link above, only 13% separated best from worst compression.
  2. From your tests, in this instance, FLIF is not truly lossless, recreating the file exactly.
  3. There are compression formats designed to recreate an original file verbatim, using a checksum or other integrity verification. A PeaZip benchmark test showed ARC 4, 7Z, RAR, ZIPX and ZPAQ can all produce comparable compression of their test file. That said, already compressed image information has data that is too random to shrink much with another compression scheme.

In summary, using truly lossless compression (i.e., not relying on use of visual subtleties), it's unlikely you could shrink a PNG more than a few percent.