Reputation: 163
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
Upvotes: 2
Views: 17759
Reputation: 1
String applicationloader = TenantLoader_lnk.getAttribute("src");
BufferedImage expected = ImageIO.read(new File("local image file path"));
BufferedImage actual = ImageIO.read(new URL(applicationloader));
if(actual.getWidth()==expected.getWidth() && actual.getHeight()==expected.getHeight());
{
for(int x=0;x<expected.getWidth();x++)
{
for(int y=0;y<expected.getHeight();y++)
{
assertTrue(actual.getRGB(x, y)==expected.getRGB(x, y), "Image is not same hence failing");
}
}
}
Upvotes: -1
Reputation: 1
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 = ImageIO.read(fileA); // reads fileA into bufferedImage
DataBuffer dbA = biA.getData().getDataBuffer();
int sizeA = dbA.getSize();
BufferedImage biB = ImageIO.read(fileB); // 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
}
Upvotes: 0
Reputation: 21
You can try to use applitools ( http://applitools.com/) 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:)).
Upvotes: 0
Reputation: 4333
Using java to compare 2 images:
BufferedImage imgA = ImageIO.read(new File("C:/img/picA.jpg"));
BufferedImage imgB = ImageIO.read(new 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
Upvotes: 7