Search code examples
imagemagick-convert

ImageMagick convert thumbnail output differs based on file name


I noticed that the binary output of convert -thumbnail differs based on file name (or inode or other metadata), it should be noted that visually there is no difference. Here is a bash script that copies a file and generates a thumbnail:

#! /bin/bash
cp avatar.png avatar2.png
convert avatar.png -thumbnail 100x100 a.png
convert avatar2.png -thumbnail 100x100 b.png
diff a.png b.png

It outputs: Binary files a.png and b.png differ

I am surprised at this behaviour, and it raises two questions:

  • out of curiosity, is there some degree of freedom in the png format? and where?
  • for testing purpose, I need a stable output. Is it achievable? For example could I set a seed?

I have tried setting -seed 123456 but the output still differ.

I am using ImageMagick version 6.9.10-23 Q16 x86_64 20190101 on Ubuntu 20.04

avatar.png: enter image description here
avatar2.png: enter image description here
a.png: enter image description here
b.png: enter image description here


Solution

  • Answer

    As explained by fmw42, the difference is in the metadata, it can be removed by using the -strip option. This script shows that the output is now stable, it shows that the produced files are equal byte for byte:

    #! /bin/bash
    cp avatar.png avatar2.png
    convert avatar.png -seed 123456 -thumbnail 100x100 -strip a.png
    sleep 3 #delay in order to force different modify timestamp
    convert avatar2.png -seed 123456 -thumbnail 100x100 -strip b.png
    diff a.png b.png
    

    Additional learnings

    identify

    One can use identify to explore an file's format:

    identify -verbose a.png
    

    Without strip, file creation time and original file URL were stored in the produced png. Surprisingly, when using strip, we can still see the file creation and modification time, but it is not stored in the png, it is coming from the filesystem.

    compare

    One can use compare to compute the visual difference between two images:

    compare -metric rmse a.png b.png diff.png