How to compare two images using Selenium

how to compare two images using sikuli in selenium
how to verify images in selenium webdriver
image comparison automation
webdriver-image comparison
how to compare two web pages in selenium
selenium image recognition
ashot selenium
selenium visual diff

I like to compare two images. imagemagic tool does this for me using cmd prompt. it compares two images (for e.g. a button's position is different) and output the result image in new gif image which highlights the difference. However i want some tool which generate the new image like this however only if there is a difference. Please suggest how can i do it using any tool or even if this is possible using selenium with java. imagemagic generate the new result image even if there is no difference between them.

edited: I have done RnD and concluded that the imagemagic + iam4java can be used through selenium to compare the images but still cannot find how to put condition of generating the output image only if there is a difference

Using java to compare 2 images:

    BufferedImage imgA = File("C:/img/picA.jpg")); 
    BufferedImage imgB = File("C:/img/picB.jpg"));   

    boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
     for (int x = 0; x < img1.getWidth(); x++) {
      for (int y = 0; y < img1.getHeight(); y++) {
       if (img1.getRGB(x, y) != img2.getRGB(x, y))
        return false;
     } else {
        return false;
     return true;

To produce the difference image you can do something like this:

    private static void subtractImages(BufferedImage image1, BufferedImage image2) throws IOException {
    BufferedImage image3 = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    int color;
    for(int x = 0; x < image1.getWidth(); x++)
        for(int y = 0; y < image1.getHeight(); y++) {
            color = Math.abs(image2.getRGB(x, y) - image1.getRGB(x, y));                
            image3.setRGB(x, y, color);
    ImageIO.write(image3, "bmp",  new File("image.bmp"));

Source of subtractImages method

Source of bufferedImagesEqual method

Gist with a working example

Compare Images in Selenium Test, Please suggest how can i do it using any tool or even if this is possible using selenium with java. imagemagic generate the new result image  Selenium WebDriver does have features to capture screenshots or images from the application under test, however, it does not have the feature to compare the images. In this recipe, we will create an extension class for comparing images and use it in our Selenium tests.

You can try to use applitools ( for image comparison testing. It saves images on its server and print link to the image in log only in case of faillure. So if your test passes (images are the same) - you'll just see a green test. If there is a difference between images - test will fail and you'll see in the log the link to applitools where you'll get your image with highlighted areas of differences and will be able to see baseline image(to compare with your own eyes:)).

How to compare two images in Java, Selenium WebDriver offers Image Comparison Testing with Java programming. of Image comparison testing through automation using Selenium program which is guiding start to compare two images and make test result  Scenario# 3: Image Comparison Testing between two images with Selenium WebDriver. This is the title of this blog post. Here we have to compare two images bit by bit through Selenium WebDriver and Java.

Here is what I have done... Open image and store Images as File Java IO

NewImage= new File(NewImagePath) //gets newImage from path NewImagePath
Original= new File(OriginalPath) // gets Original from pathOriginalPath

Created a function "getImagePercentage", that compares both the image and retruns machines %percetage, 100% if both images are same else any other value.

Call this method as : getImagePercentage(Original,NewImage);

    getImagePercentage(File fileA, File fileB) { // start of the function getImagePercentage

    def percentage = 0;
    BufferedImage biA =; // reads fileA into bufferedImage
    DataBuffer dbA = biA.getData().getDataBuffer();
    int sizeA = dbA.getSize();
    BufferedImage biB =; // reads fileA into bufferedImage
    DataBuffer dbB = biB.getData().getDataBuffer();
    int sizeB = dbB.getSize();
    int count = 0;
    // compare data-buffer objects //
    if (sizeA == sizeB) { // checks the size of the both the bufferedImage

        for (int i = 0; i < sizeA; i++) {

            if (dbA.getElem(i) == dbB.getElem(i)) { // checks bufferedImage array is same in both the image
                count = count + 1;
        percentage = (count * 100) / sizeA; // calculates matching percentage
    } else {
        System.out.println("Both the images are not of same size");
    return percentage; // returns the matching percentage value

Image Comparison in Selenium WebDriver, Is it possible to compare two screenshots of the same page taken at time t1 and We started by using Selenium WebDriver to capture a baseline screenshot of  Create comparison logic for screenshots. When you compare screenshots you have 2 options : full page comparison and panel comparison. For .Net there is a very nice library to compare images Simple image comparison in .NET. It’s a very powerful image and the integration in any Selenium WebDriver project can be don quite easy.

Image Comparison Testing With Selenium WebDriver And Java, How can we run a Test method multiple times in a loop(without using any data provider)? Ans. Using invocationCount parame Live Traffic  Selenium does not provide a way to compare images. You will need some other mechanism to do that. This does not mean you cannot use Selenium for your project; rather, it means you will need to use Selenium to capture the images and something other than Selenium to compare the images. – user246 Apr 27 '16 at 14:55 2

Image Comparison in Selenium-Selenium Webdriver Appium , Let's implement the CompareUtil class with a method to compare two image files, as shown in the following code: Copy. import java.awt.Image; import java.awt. The next source code snippet is using the just introduced method to compare images. At first a webpage is opened and a screenshot is taken. import org.openqa.selenium.WebDriver ; import org.openqa.selenium.firefox.FirefoxDriver ;

Image Comparison Made Easy, In this blog, we are going to learn, how we can compare two images using Third-​party utility Selenium Shutterbug. I would highly recommend referring to this blog​  I am currently working in Selenium WebDriver to compare two images during automation. Currently I am using pixel comparison but the problem comes if the browser size is changed or the system is different on which I run the automation.

  • Thanks for the suggestion Anna.
  • Is applitools free? or commercial?