Create the same PNG with ImageMagick 2 times: Binaries differ

binaryimagemagickpngversion control

I create two images that should be identical, but their binaries are different:

$ convert -size 1x1 xc:white out1/w.png
$ sleep 1
$ convert -size 1x1 xc:white out2/w.png
$ diff out1/w.png out2/w.png 
Binary files out1/w.png and out2/w.png differ

Probably because of a timestamp in embedded metadata.

QUESTION: How to make ImageMagick create a binary that will always be the same?


Context

I have a big ImageMagick script that creates many images that are then saved to Git (because most developers don't have the environment necessary to run the script).

I often edit the script (ex:define a new image) and then run it to regenerate all images. But I don't want to have Git differences for images that have not changed.

Apparently some compression algorithms produce slightly different results on different architectures. Not a big problem since I always generate on the same machine. But even on the same machine, the files always all differ.

ImageMagick 6.8.9-9 Q16 x86_64 2015-08-06, on Ubuntu 2015.10

Best Answer

It should be possible to strip the timestamps, using -define png:exclude-chunks=date,time; but there's apparently a bug in the PNG encoder.

If you can stage your modifications, one possibility would be to use identify's signature to only replace an existing image if the pixel contents of the image change:

mkdir out
convert -size 1x1 xc:white out/w.png
if [ ! -f w.png ]; then
    mv out/w.png w.png
else
    if [ "$(identify -verbose w.png | awk '/ignature/ { print $2 }')" != "$(identify -verbose out/w.png | awk '/ignature/ { print $2 }')" ]; then
        mv out/w.png w.png
    else
        rm out/w.png
    fi
fi
Related Question