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
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"));