30

I am looking for a way to get the Bitwise XOR of two images on the command line(or in another way that can be implemented in a program or script).

This should result in the same final picture as using the XOR Blending mode in picture editors that support it (Paint.NET, Photoshop, etc)

As an example, say I have Image A:

Image A(Seattle, from the paint.net documentation

and Image B:

ImageB(Apple, from the paint.net documentation)

then the result should look like:

Image C(Xor result of above images, from the paint.net documentation)

The fun part of this is of course, that when you XOR image C with image B again, you will get an exact copy of image A.

Now, I have been looking all over the internet for a way to do this programmatically, but I have found nothing. Even ImageMagick does not support doing a bitwise XOR on images.

Does sombebody know a way to do this?

0

6 Answers 6

35

ImageMagick can do it, although it's a bit convoluted. One way is:

convert img1 img2 -fx "(((255*u)&(255*(1-v)))|((255*(1-u))&(255*v)))/255" img_out

(img1,img2,img_out are the two input and single output file names respectively).

Explanation

It's a bit ugly (I'm sure someone with more ImageMagick-fu than me could clean it up but it works like this:

  1. -fx "xxx" basically says "perform the operation xxx on the image". In the expression above, u and v stand for the first and second input images respectively.

  2. Now, -fx only has bitwise AND & and bitwise OR | in the way of bitwise operators. To reconstruct bitwise XOR, we need

    convert img1 img2 -fx "(u & NOT v) | (NOT u & v)" img_out
    
  3. To get the NOT (there is a logical NOT but no bitwise NOT), we remember that NOT x = 255-x if x is 8-bit. So to get NOT u we can just do 255-u, assuming image u is 8-bit. Hence, the ImageMagick command would be:

    convert img1.png img2.img -fx "((255-u)&v)|(u&(255-v))" image_xor.png
    
    • The one problem here is that when ImageMagick does fx it normalises all the pixels in u and v in the range [0,1] instead of [0,255] as we expect, and doing bitwise on non-integers screws stuff up.

    • Hence, we have to multiply all occurrences of u and v in the above expression by 255 (so the bitwise operations work), and divide by 255 at the very end to get back in the range [0,1] that ImageMagick expects.

This gives us the original command,

convert img1 img2 -fx "(((255*u)&(255*(1-v)))|((255*(1-u))&(255*v)))/255" img_out

Voila!

3
  • 2
    That looks amazing ;D. There was a small error in your code tough (the second u should be a v). Fixed it. It does however take AGES to run... ^^'
    – Qqwy
    Dec 14, 2011 at 14:26
  • ta, shame it takes so long to run though (I only tried it on those baby images you have). Writing some code would still be fastest, -fx in imagemagick does have a reputation for being slower. Dec 14, 2011 at 23:18
  • ImageMagick-7.0.11-Q8>magick.exe 1.png 2.png -fx "u&(1-v)|(1-u)&v" img.png (tested for 1 bit and 8 bit images)
    – qqqq1961
    May 24, 2021 at 9:42
27

I found a need for xor on an image and the G'MIC tool works for me. G'MIC is incredibly powerful, as is Image Magick, but worth a look for solving some tough image processing problems.

gmic a.png b.png -blend xor -o result.png

G'MIC also works directly on the images posted above.

gmic https://i.stack.imgur.com/Ws6e8.png https://i.stack.imgur.com/hoBIM.png -blend xor -o result.png

For help,

gmic -h -blend

enter image description here

1
  • 2
    It is very unfortunate that only one answer can be selected, as this gmic looks like a wonderful tool. (I selected the answer that I ended up using originally, which I should have but forgot to select two years ago). Thank you for this great answer!
    – Qqwy
    Oct 15, 2016 at 22:10
7

Here is how I would do in Java:

Iterate over all the pixels of the two images at once. (for loop (x) inside a for loop (y)). Of course, use a BufferedImage. You can get the color of the pixel by doing:

int color = img.getRGB(x, y);

Do the same for the other image as well and perform the xor operation on the two colors. Store the resulting value in a new BufferedImage with the same dimensions as the two input images.

Here is some sample code:

public static BufferedImage xorEffect(BufferedImage imageA, BufferedImage imageB) {
    if (imageA.getWidth() != imageB.getWidth() ||
        imageA.getHeight() != imageB.getHeight())
    {
        throw new IllegalArgumentException("Dimensions are not the same!");
    }
    BufferedImage img = new BufferedImage(imageA.getWidth(),
                                          imageA.getHeight(),
                                          BufferedImage.TYPE_INT_ARGB_PRE);

    for (int y = 0; y < imageA.getHeight(); ++y) {
        for (int x = 0; x < imageA.getWidth(); ++x) {
           int pixelA = imageA.getRGB(x, y);
           int pixelB = imageB.getRGB(x, y);
           int pixelXOR = pixelA ^ pixelB;
           img.setRGB(x, y, pixelXOR);
        }
    }
    return img;
}

To load an image from a file use:

BufferedImage imageA = ImageIO.read(new File("/home/username/image.png"));
2
  • You might want to use BufferedImage.TYPE_INT_RGB, so that alpha does not cause differences. Feb 23, 2015 at 21:58
  • 1
    I think it should be: int pixelXOR = pixelA ^ pixelB ^ 0xFF000000; Apr 17, 2022 at 22:52
3

If you want to do yourself, do it pixel by pixel. If you want a library, I recommend OpenCV. This is very nice and open source library with huge operations supported in image processing area. It supports direct XOR using ^ operator. Good Luck.

1

Knowing that

A XOR B = (A AND NOT B) OR (NOT A AND B).

and that most of common image processing tools do have and, or and not operations the rest is quite easy :)

Working in Python, you could have a simple script performing the operation and even adding it as a plugin in the gimp ;)

1
  • 1
    BTW, I might have a quick and (somehow) elegant implementation using Python and opencv. I could send it here if want need it ;)
    – jlengrand
    Dec 14, 2011 at 15:20
1

OpenCV has all the logical operators on images and numpy images using bitwise_# Where # can be xor, and, and, not ... See OpenCV - Intersection between two binary images

1
  • I'd just want to XOR an image not against another image but against a binary key. I see OpenCV methods need two images...how could I get this? Thanks a lot EDIT: Well, I've seen gmic.eu/reference/xor.html. I'll try it
    – Osqui
    Sep 24 at 9:20

Your Answer

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

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