← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #177994]: Comparing screenshots dynamically

 

Question #177994 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/177994

    Status: Open => Answered

RaiMan proposed the following answer:
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.

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.