compare 2 images and see whether the images are same or not

Asked by Mohmyda

Hi,

Request your help to provide the solution/effort regarding Image comparison through Sikuli. We have to compare 2 images and see whether the images are same or not.

If we write a function in Sikuli then can this be called from QTP or not.

Regards,
Mohmyda

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Mohmyda
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

If your use of Sikuli script is reduced to this function, it my not be the right tool for you:
- comparing images can be accomplished by using the Finder feature (see docs), but this is not the focus of Sikuli: it tries to find images on a screen, by searching for them in screenshots, that are taken internally.
- as far as I understand QTP, it will be not possible, to directly call a function that is written with Sikuli script (although there are more or less complex solutions for integrating Sikuli in applications, that are developed with Java and/or Python/Jython (but this is not your situation))

This could be possible:
write and test a script using the Sikuli IDE, that compares 2 images based on the Finder feature and make it a .skl. It can be made flexible, by using command line parameters, to specify the todo and the pictures to use. Then start this .skl from your testscript as an external new process using the Sikuli.bat. The problem will be, that in the moment, you will not get back any usable information, so you have to implement a communication between the Sikuli (using standard Python) and your test scripts (e.g. using a log file).

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#2

Hi RaiMan,

1. The below is the example for finder feature, I am correct?

 f = Finder("path-to-an-imagefile") # create a Finder with your saved screenshot
img= "path-to-an-imagefile" # the image you are searching
f.find(img)

2.I am unable to save it using .skl

I didnot understand your rest of the steps

please explain step by step with an example, what is the exact problem we face during image comparison.

3. and you mean to say we can not use QTP and call the sikuli script(please provide the reason in layman terms)

Thanks in advance

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Thanks for a new word: layman ;-)

1. yes, but more according to my proposed solution:

import sys
img1 = sys.argv[1] # contains the absolute-path-to-image-file-1
img2 = sys.argv[2] # contains the absolute-path-to-image-file-2
f = Finder(img1)
if f.find(img2).hasNext():
    print("we found it")
else:
    print("we did not find it")
f.destroy()

2. you have to use menu File -> export as executable (Shift-Ctrl-E). this produces a .skl.

3. you ask: you mean to say we can not use QTP and call the sikuli script?
Of course can you do it. from your test script just use
VB-Script-Run-on-commandline "Path-to-Sikuli.bat sik-test1.skl absolute-path-to-image-file-1 absolute-path-to-image-file-2"
where VB-Script-Run-on-commandline is the command to use in your test script to get something running from the command line. the test script has to pause until the .skl has finished (sychronous execution).

One more thing:
If you get this running, there will be no information available about the result of the comparison, since the .skl does not return any information (e.g. return code), that can be evaluated in your test script.
a simple solution for that could be to redirect the output of the .skl (containing the text produced by the print's) to a file and then afterwards inspect the contents of this file in your testscript:

VB-Script-Run-on-commandline -
"Path-to-Sikuli.bat sik-test1.skl absolute-path-to-image-file-1 absolute-path-to-image-file-2 >absolute-path-to-sik-test1-log.txt"
# then (in fantasy language)
f = open file "absolute-path-to-sik-test1-log.txt"
if f.contains("found):
     print "success"
else:
     print "go to hell!"

hope this helps.

If you need more help/information, you are welcome to come back.

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#4

Thank you,

I am pretty clear on this now

I will work out on this, if any doubts in between. I will definitely get back to you

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#5

Hi RaiMan,

On executing this below script, it throws the following message. please let me know what might be the problem

import sys
img1 = 'C:\\Program Files\\Sikuli\\s1.jpg'
img2 = 'C:\\Program Files\\Sikuli\\s2.jpg'
#img1 = sys.argv[1] # contains the absolute-path-to-image-file-1
#img2 = sys.argv[2] # contains the absolute-path-to-image-file-2

f = Finder(img1)
if f.find(img2).hasNext():
    print("we found it")
else:
    print("we did not find it")
f.destroy()

[sikuli] Stopped
[sikuli] An error occurs at line 8
[sikuli] Error message:
Traceback (most recent call last):
  File "C:\DOCUME~1\himabindu\Local Settings\Temp\sikuli-tmp62504.py", line 6, in <module>
    f.find("1282043246046.png").hasNext()
AttributeError: 'NoneType' object has no attribute 'hasNext'

2. If I remove hasNext() from the script , script executes properly but say's "we did not find it" for all the image comparisons.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

Sorry for misleading: I made a mistake (... and I have written the docs - LOL)

this is the correct version:

import sys
img1 = sys.argv[1] # contains the absolute-path-to-image-file-1
img2 = sys.argv[2] # contains the absolute-path-to-image-file-2
f = Finder(img1)
f.find(img2) # searches and stores the matches in the Finder object f
if f.hasNext(): # is None if nothing found
    print("we found it")
else:
    print("we did not find it")
f.destroy()

one more thing:
It seems, that with version 10.2, the value with exit(value) now is returned to the command line. So you could use
if f.hasNext():
    exit(0)
else:
    exit(1)
in your final version (.skl) and check in your test script for the returned value.
(I did not test it yet!)

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#7

Hi RaiMan,

Thanks for your Help. This is working fine

But when I try to execute from command line . It throws the below exception

C:\Documents and Settings\himabindu>"C:\Program Files\Sikuli\Sikuli-IDE.bat" welcom.skl
Can't open file: welcom.skl
java.io.FileNotFoundException: welcom.skl (The system cannot find the file speci
fied)

I Placed this file in "C:\Program Files\Sikuli\"

Do I need to place any entry in the "Sikuli-IDE.bat" file (What needs to be done)?

Revision history for this message
RaiMan (raimund-hocke) said :
#8

You should not store anything into C:\Program Files\Sikuli\, since this dir may be overwritten with the next installation.

use any dir in your normal working path for storing your .sikuli and .skl.

assumed, your .skl resides in C:\SomeDir\welcom.skl

in the command prompt window you should say:
cd C:\SomeDir
C:\Program Files\Sikuli\Sikuli-IDE.bat welcom.skl

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#9

Thank You,

Works perfectly.

Revision history for this message
Mohmyda (mohana-kandaswamy) said :
#10

if f.hasNext():
    exit(0)
else:
    exit(1)

also works fine

Revision history for this message
Ashok kumar (ashok14) said :
#11

Hi Mohmyda,

How did you compare two images? Using Sikuli IDE or Using the application that are developed with Java, python..etc.

Can you please explain, I got struck at comparing two images.

In Automation(Selenium webdriver), I am trying to compare two images.

My frame work was developed in Java.

Thanks in Advance.

Regards,
Ashok.