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%.
- 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