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.
    – user287001
    Jan 7 '17 at 0:09

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
    Jan 7 '17 at 0:24
  • That's what the commands give you. Is the second (red on transparent) image not what you are looking for? Jan 7 '17 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
    Jan 7 '17 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. Jan 7 '17 at 0:31
  • 1
    Perfect, lots of thanks. Managed to do it by adding this command: magick composite -compose copy_opacity alpha.png original.png result.png
    – ManuelAMS
    Jan 7 '17 at 0:51

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, privacy policy and cookie policy

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