# How to compare two images

**URL:** <https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353>\
**Category:** Eggplant Functional\
**Created:** [July 17, 2013, 4:28am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353 "2013-07-17T04:28:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitalu](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@vitalu](https://forums.eggplantsoftware.com/u/vitalu)\
**Post date:** [July 17, 2013, 4:28am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/1 "2013-07-17T04:28:05Z")

</div>

Hi All,  
I need to compare two screenshots of SUT.  
First I capture the screenshot 1 for application 1 after made some changes again I need to capture the screenshot 2.  
Now I have to compare the two screenshots.  
If data of screenshots same then it will log correct data otherwise, logerror like data has been modified.

Please suggest me.

---

<div class="post-metadata">

**Author:** ![trinadhu](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/t/9fc348/32.png) [@trinadhu](https://forums.eggplantsoftware.com/u/trinadhu)\
**Post date:** [July 17, 2013, 5:18pm UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/2 "2013-07-17T17:18:20Z")

</div>

Here’s your script-

* * *

If ImageFound(4.0, “Image1”)  
Log “Image 1 found”  
else  
LogWarning “Image1 not found, please check”  
end if

wait 10.0

If ImageFound(5.0, “Image2”)  
Log “Image 2 found”  
else  
LogWarning “Image 2 not found, Please check”  
end if

If “Image1” = “Image2”  
Log “Images found same - no further changes observed”  
else  
LogError “Images not identical”  
end if

* * *

You also need to make sure to modify Wait Time based on screen navigations. Also have a look below link which details using expressions in EggPlant - [http://docs.testplant.com/?q=content/expressions](http://docs.testplant.com/?q=content/expressions)

---

<div class="post-metadata">

**Author:** ![vitalu](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@vitalu](https://forums.eggplantsoftware.com/u/vitalu)\
**Post date:** [July 18, 2013, 3:47am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/3 "2013-07-18T03:47:22Z")

</div>

Hi ,

I am capturing the image while running the script.  
I came to know it is not possible to compare the images

---

<div class="post-metadata">

**Author:** ![trinadhu](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/t/9fc348/32.png) [@trinadhu](https://forums.eggplantsoftware.com/u/trinadhu)\
**Post date:** [July 18, 2013, 4:35am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/4 "2013-07-18T04:35:58Z")

</div>

That’s correct. You cannot manually capture images while running the script.

Even you cannot edit the script while running the tool. You can only able to edit the script in main window but not in running window while execution in process.

---

<div class="post-metadata">

**Author:** ![SenseTalkDoug](https://forums.eggplantsoftware.com/user_avatar/forums.eggplantsoftware.com/sensetalkdoug/32/88_2.png) [@SenseTalkDoug](https://forums.eggplantsoftware.com/u/SenseTalkDoug)\
**Post date:** [July 18, 2013, 11:02am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/5 "2013-07-18T11:02:13Z")

</div>

There are at least two ways to compare two screenshots of the SUT. The first is to capture the original image and then search for it again later, possibly using a searchRectangle to look only in the place where you expect it to appear. If it is found then you can log success.

The other approach is to capture the same area of the screen at two different times into two image files and compare those files. Here’s how it would look:

```auto
set areaOfInterest to (100,100,300,300) -- the area of the screen to compare
captureScreen "/tmp/originalImage", areaOfInterest

-- do some other processing

captureScreen "/tmp/laterImage", areaOfInterest -- capture the same area again

if file "/tmp/originalImage.tiff" as data is equal to file "/tmp/laterImage.tiff" as data then
    logSuccess "The images are the same"
else
    logFailure "The images are different"
end if

```

Note that if you’re running on Windows or Linux the images will be captured as .png files not .tiff, and on Windows you’ll need to store them in a different directory, not /tmp. But if you’re careful to get the image file names correct, this approach should work. It’s also important that you compare the files “as data” so SenseTalk won’t try to convert them to text strings which would give unreliable results when comparing binary data.

---

<div class="post-metadata">

**Author:** ![vitalu](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@vitalu](https://forums.eggplantsoftware.com/u/vitalu)\
**Post date:** [July 25, 2013, 6:39am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/6 "2013-07-25T06:39:12Z")

</div>

Thanks for this solution. this is working fine for me.  
Now, I want to compare with 3 screenshots.

I have one defaults image , image x , image y

first I have to compare with default image with image x  
if those are same log “sucess” not same "logerror “fail”

second condition compare with imagex and imagey

second condition is working fine as you given example in last forum.

here, I am trying to execute like this but I am getting error please verify and send me correct code

```auto

if file "/tmp/default.tiff" as data is equal to file "/tmp/imagex.tiff" as data then 
    logerror "The images are not same" 
else if file "/tmp/x.tiff" as data is equal to file "/tmp/y.tiff" as data then
log " the images are same"
else 
    logFailure "The images are different" 
end if
 

```

---

<div class="post-metadata">

**Author:** ![eggElizabeth](https://forums.eggplantsoftware.com/user_avatar/forums.eggplantsoftware.com/eggelizabeth/32/92_2.png) [@eggElizabeth](https://forums.eggplantsoftware.com/u/eggElizabeth)\
**Post date:** [July 31, 2013, 5:54am UTC](https://forums.eggplantsoftware.com/t/how-to-compare-two-images/3353/7 "2013-07-31T05:54:59Z")

</div>

I think you will want to use “LogError” instead of “LogFailure” towards the end. The LogError command will cause the script to log as a failure if it is executed.

If this does not resolve your problem, can you tell us what error message it is that you are seeing?
