2012-04-16 14:00

After searching a long time on the Net, here is how to use ImageMagick to compare two images (diff), to determine if the images are similar, or if one image is a resize version of the other.

convert image1 image2 -resize '400x300!' MIFF:- | compare -metric AE -fuzz '10%' - null:

The convert command takes 2 images and scale them to a smaller identical size. We then pipe them to the compare command. The compare command will count the number of different pixels.

The command displays the number of different pixels. If it’s zero then pictures are similar.

2 parameters can increase the similarity tolerance:

  • The size of thumbnails to compare: the smaller it is, the more details are removed. You can use 1/4 of the smaller image for example.
  • The -fuzz parameter: the color distance tolerance. The more you increase this param, the more you allow different colors. Color difference is almost undetectable below 2%.

Notes:

  • It’s better to keep ratio while generating thumbnails.
  • The exclamation mark is needed to force image scale without preserving ratio. Otherwise in some cases the 2 thumbs are not strictly the same size and the compare command fails.

I also made a small script to scale at 1/4 of the small image and displays the percentage of different pixels: imdiff

./imdiff /tmp/bad.jpg ../Public/images/bad.jpg
pixel difference: 2.927%
NOK

Links:

2012-04-16 14:00 · Tags: , ,

6 Comments

  1. the link for imdiff is not working anymore…. :-(

    Reply

  2. thanks for your feedback.
    I’ve fixed it.

    Reply

  3. Nice, that solved my problem! Thank you!

    Reply

  4. Very useful. Thanks for sharing.

    Reply

  5. Very useful, thanks a lot!!!

    I am an absolute newbie to Linux, let alone scripting. I need to compare two images without re-sizing them, and output should be used by script for the following calculations (that is I don’t need to see the difference visually).

    A relevant example I found is:

    compare -metric MAE image1 image2 null: 2>&1

    I changed it to:

    compare -metric MAE rose1.jpg rose2.jpg null: 2>&1 (I have no idea what ‘null: 2>&1′ means, would be grateful if you could explain it),

    but I got an error – cannot find the files or they don’t exist. Is it because I need to specify full path to the images? or something else is wrong in this script?

    Could you help me out please?

    Reply

  6. Actually I got ‘missing an image filename’

    Reply

Leave a Reply to Marius Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>