“Diff” an image using ImageMagick

My own favorites are these two:

 compare image1 image2 -compose src diff.png
 compare image1 image2 -compose src diff.pdf

The only difference between the 2 commands above: the first one shows the visual difference between the two images as a PNG file, the second one as a PDF.

The resulting diff file displays all pixels which are different in red color. The ones which are unchanged appear white.

Short and sweet.

Note, your images need not be the same type. You can even mix JPEG, TIFF, PNG — under one condition: the images should be of the same size (image dimension in pixels). The output format is determined by the output filename’s extension.

Should you, for some reason, need a higher resolution than the default one (72 dpi) — then just add an appropriate -density parameter:

 compare -density 300 image1 image2 -compose src diff.jpeg

Illustrated examples

Here are a few illustrations of results for variations of the above command. Note: the two files compared were even PDF files, so it works with these too (as long as they are 1-pagers)!


Left: Image with text       Center: Original image      Right: Differences (=text) in red pixels.
Red difference pixels only; identical pixels are white

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -compose src \
        diff-compose-default.pdf

This is the same command I suggested earlier above.


Left: Image with text      Center: Original image      Right: Differences in ‘seagreen’ pixels.
Seagreen difference pixels only; identical pixels are white

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -compose src \
       -highlight-color seagreen \
        diff-compose-default.pdf

This command adds a parameter to make the difference pixels ‘seagreen’ instead of the default red.


Left: Image with text       Center: Original image       Right: Blue diffs (but w. some context background)
Blue difference pixels only; first of the compared images as a lightened-up backgroundl

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -highlight-color blue \
        diff-compose-default.pdf

This command removes the -compose src part — the result is the default behavior of compare which keeps as a lightened background the first one of the 2 diffed images. (This time with added parameter to make the diff pixels appear in blue.)

Leave a Comment