title: Comparing 2 images with transparency area in Java
body: <p>For an automation tool I'm working on I need to compare 2 images.
  My code works perfectly when I have &quot;normal&quot; images, but it fails when one of the images has a transparent area.</p>
  <p>I'm reducing the color between the images for each pixel and creating a negative image that shows the difference. For images with transparent area the negative image is whole white, I can't see any shape or other info.</p>
  <p>How to ignore the transparent area (shown in the images as gray color)?</p>
  <p><a href="https://i.stack.imgur.com/Gmlc9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gmlc9.png" alt="First image" /></a>
  <a href="https://i.stack.imgur.com/GvB0G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GvB0G.png" alt="Second image" /></a></p>
  <p>My code:</p>
  <pre><code>  private static BufferedImage createDiffImage(BufferedImage img1, BufferedImage img2) {
      BufferedImage result = new BufferedImage(img1.getWidth(), img1.getHeight(), img1.getType());
      int color;
      for(int x = 0; x &lt; img1.getWidth(); x++)
          for(int y = 0; y &lt; img1.getHeight(); y++) {
              color = Math.abs(img2.getRGB(x, y) - img1.getRGB(x, y));
              result.setRGB(x, y, color);
          }
      return result;
  }
  </code></pre>