Comparing screenshots dynamically

Asked by Hari Kiran Vankayala

Hi Raiman,

I would like to compare screenshots one is static (on my desktop) and second is from a website (after couple of operations)

Can sikuli do clear and precise image comparision?

Please check my code.
The issue here is the image with which i am comparing to the image from web page, i have knowingly did small change so that sikuli can capture but I am always getting 'Matching Image'

f = Finder("/Users/hari/Desktop/DesktopImage.png")## Expected Image, did small change so that case will Fail
setX(888)
setY(87)
setW(500)
setH(380)
x = getX()
y = getY()
w = getW()
h = getH()
img = capture(x,y,w,h)
print img

f.find(img) # find all matches

if f.hasNext():
        print "Image is matching"
else:
        print "Image is not matching"
f.destroy()

Question information

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

principally ok.

but the set's and get's are not really needed ;-)

f = Finder("/Users/hari/Desktop/DesktopImage.png")

# comment 1
capreg = Region(888, 87, 500, 380)

# comment 2
img = capture(capreg)

f.find(img) # find all matches
if f.hasNext():
        print "Image is matching"
else:
        print "Image is not matching"
f.destroy()

-- comment 1
for testing I recommend this. later you might want to replace Region() by any other construct (eg. some calculations based on the app window) to get capreg in the end.

-- comment 2
print img does not help much, because it points to a temp folder and will be deleted at script end. If you want to save it for documentation:

# at beginning of script
import os
import shutil
savedir = "/Users/hari/Desktop/savedimages"

# to save image
shutil.move(img, os.path.join(savedir, str(int(time.time()))+".png")
# saves it with a timestamp: 1320736301.png

*** putting all together
# settings
import os
import shutil
dir = "/Users/hari/Desktop"
savedir = os.path.join(dir, "savedimages")

# action
baseimg = os.path.join(dir, "DesktopImage.png")
capreg = Region(888, 87, 500, 380)

img = capture(capreg)
shutil.move(img, os.path.join(savedir, str(int(time.time()))+".png")

f = Finder(baseimg)
f.find(img) # find all matches
if f.hasNext():
        print "Image is matching"
else:
        print "Image is not matching"
f.destroy()

--- the action block can now be easily packed into a def, that returns either True or False.

def compare_images(baseimg, capreg):
    img = capture(capreg)
    shutil.move(img, os.path.join(savedir, str(int(time.time()))+".png")
    f = Finder(baseimg)
    f.find(img) # find all matches
    ret = False
    if f.hasNext(): ret = True
    f.destroy()
    return ret

if compare_images("DesktopImage.png", Region(888, 87, 500, 380)):
     print "match"
else:
    print "no match"

*** Take care, that the captured image's width and height are both less or equal compared to the base image. But sorry, there currently is no easy way, to get the size of a loaded image.

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#2

Hi Raiman,

I ran the below script

click("App.png")
# settings
import os
import shutil
dir = "/Users/hari/Desktop"
savedir = os.path.join(dir, "savedimages")

# action

baseimg = os.path.join(dir, "Expected.png")
capreg = Region(887,85,500,380)

img = capture(capreg)
shutil.move(img, os.path.join(savedir, str(int(time.time()))+".png")

f = Finder(baseimg)

f.find(img) # find all matches
if f.hasNext():
        print "Image is matching"
else:
        print "Image is not matching"
f.destroy()

I am facing the below issue, could not able to solve. Sikuli is stopping at f = Finder(baseimg)
Can you please help.

[error] Stopped
[error] An error occurs at line 16
[error] Error message:
SyntaxError: ("no viable alternative at input 'f'", ('/var/folders/7O/7OYo3pwNFseqmd9waLgz3E+++TI/-Tmp-/sikuli-tmp14715735451173188.py', 16, 0, 'f = Finder(baseimg)\n'))

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

sorry, my fault.

The Python syntax checker is a little bit dumb. If more intelligent, he could have told you, that at the point it stops ( the f in the line of code), it is missing a closing bracket at this point, which means in the statement before.

correct:
shutil.move(img, os.path.join(savedir, str(int(time.time())))+".png")

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#4

Hi Raiman,

The above issue is resolved but when i execute the below code.

# settings
import os
import shutil
dir = "/Users/hari/Desktop"
savedir = os.path.join(dir, "savedimages")

# action

baseimg = os.path.join(dir, "desktopimage.png")

capreg = Region(887,85,512,384)
img = capture(capreg)
shutil.move(img, os.path.join(savedir, str(int(time.time())))+".png")

f = Finder(baseimg)

f.find(img) # find all matches
if f.hasNext():
        print "Image is matching"
else:
        print "Image is not matching"
f.destroy()

I am facing below error.

[error] /var/folders/7O/7OYo3pwNFseqmd9waLgz3E+++TI/-Tmp-/sikuli-scr-472915460619067946.png looks like a file, but can't be found on the disk. Assume it's text.
Image is not matching

Actually I should be getting "Image is matching", actual image and expected image are identical but still my result is negative.
Can you please let me know what is the issue.

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

LOL, rolling on the ground ....

Since we MOVE the image file, before it is used by the Finder, it can surely not be found any more in the temp folder.
So lets LOL together.

so this should do it finally:

capreg = Region(887,85,512,384)
img = os.path.join(savedir, str(int(time.time())))+".png"
shutil.move(capture(capreg), img)

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

Uuups, copy and paste error ;-)

capreg = Region(887,85,512,384)
img = os.path.join(savedir, str(int(time.time())))+".png")
shutil.move(capture(capreg), img)

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#7

Hi Raiman,
This is working finally.

Small correction, in the above code there is one extra brace.

img = os.path.join(savedir, str(int(time.time()))+".png")

This will do.

And to precisely compare two images i am using this
f.find(img,0.999)

This is successful.

Thank you,
Hari.