Anyone knows how to make imagemagick output an image with the differences between two other images? Similar to a diff, but not exactly the same. Any other Windows/Linux commandline tool that can be scripted would be good too (because I need to do this with hundreds of images).

Quick and dirty example: Let's say I have these two images:

Image A Image B

And I need to obtain only the curtain (and any other differences if there were), with everything else transparent.

  • Only Photoshop knowlwdge here. But if you have the difference PNG files with black already in one directory, you can create a PS action that walks through that directory, deletes the exact black and saves (as PNG). Possibly you need to change the background to be a layer before the deletion. Alternatively you can use a difference photo as it's own layer mask. Before saving you maybe want to make all non-black in the mask full white.
    – user82991
    Commented Jan 7, 2017 at 0:09

3 Answers 3


It's the first example in the ImageMagick documentation:

magick compare -compose src du2SX.png ejUjy.png difference.png
magick difference.png -transparent "#CCCCCC" alpha.png

In "difference.png" the identical pixels are rendered in mid-gray: enter image description here

The second command makes those pixels transparent: enter image description here

Using +transparent instead of -transparent inverts things so you get:

enter image description here

Finally, use this as a mask

magick composite -compose copy_opacity alpha.png du2SX.png result.png

to get:

enter image description here

  • Yes, that's what I was doing already, but that's not exactly what I'm asking for. I don't want a red mark telling me where the differences are, but an output with the actual differences, the curtain (in that example) as a separate image.
    – ManuelAMS
    Commented Jan 7, 2017 at 0:24
  • That's what the commands give you. Is the second (red on transparent) image not what you are looking for? Commented Jan 7, 2017 at 0:25
  • Sorry, English is not my primary language and I might have not explained it well. The output I want would be this: i.imgur.com/XH8TiXX.png.
    – ManuelAMS
    Commented Jan 7, 2017 at 0:29
  • OK you want the original curtain, so you need to turn the red part into a mask and use that to extract the curtain from the original image. Commented Jan 7, 2017 at 0:31
  • 2
    Perfect, lots of thanks. Managed to do it by adding this command: magick composite -compose copy_opacity alpha.png original.png result.png
    – ManuelAMS
    Commented Jan 7, 2017 at 0:51

Glenn's answer still mostly works six years later. You'll just need to make slight adjustments to the highlight and lowlight color.

magick compare -highlight-color "#c00018" -lowlight-color "#CCCCCC" -compose src du2SX.png ejUjy.png difference.png

I found, that the default high- and lowlight color is slightly transparent and thus won't work with the -transparent argument in the next step.

(I would have made this into a comment, but my reputation is too low. I just made this account, so people won't run into the same trouble as me.)


convert pic1.png ( -clone 0 pic2.jpg -compose difference -composite -threshold 5% -fill red -opaque white -transparent black ) -compose over -composite pic_compare.png

ImageMagick - use compare operation to generate diff image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.