Set tolerance in click(image)

Asked by Max

Hi,

I have a problem in my Sikuli python script and no idea how to solve it.

I need to click on an image, but this could slightly differ from one computer to another. My code now is something like this:

            while (exit != 1):
                count += 1
                if exists("1508670860035.png"):
                    click("1508670923333.png")
                    exit = 1
                else:

I would like to say something like "if exists("1508670860035.png").similar(0.5) click ("1508670860035.png").similar(0.5)".
So I would like to add sort of tolerance for this image, so it could not be exactly the same, but differ a little.

I tried to use similar() function but I think I used in a wrong way (my script crashed).

Any suggestion?

Question information

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

have you already looked into the docs?
learn about using a Pattern instead of an image, to control wanted similarity.

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

... but be aware:
lowering the wanted similarity to 0.5 increases the risk for false positives.
You might think of optimizing your shots (as little background as possible in most cases)
see: http://sikulix-2014.readthedocs.io/en/latest/basicinfo.html#sikulix-how-does-it-find-images-on-the-screen

Revision history for this message
Max (max883) said :
#3

thanks for the tips!

I was able to do it using pattern as you suggested with the following code:

my_image = find(Pattern("1513067174539.png").similar(0.5))
click(my_image)

Then I tried to insert exsist function since I don't know a priori if the image is present and I don't want that if the find fails the program stops, but the script crashed.

while true
    my_image = find(Pattern("1513067174539.png").similar(0.5))
    if exists(my_image):
        click(my_image)
    else:
        sleep(5)
        type("t", KeyModifier.CTRL)

How can I solve this problem? Is it possible to use a mach object obtained by find() inside exsist()???

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

exists is the same as find, but does not stop with FindFailed in case of not found.

while true
    my_image = exists(Pattern("1513067174539.png").similar(0.5))
    if my_image:
        click(my_image)
    else:
        sleep(5)
        type("t", KeyModifier.CTRL)

BTW: already know the docs http://sikulix-2014.readthedocs.io/en/latest/index.html

alternatively:
while true
    if exists(Pattern("1513067174539.png").similar(0.5)):
        click()
       # click(getLastMatch()) #with versions before 1.1.1
    else:
        sleep(5)
        type("t", KeyModifier.CTRL)

one more thing:
Your loop runs forever.
So it is only interruptible by brute force.
think about integrating some leave condition
if someCondition:
    break
see faq 1437

Revision history for this message
Max (max883) said :
#5

Thanks RaiMan, that solved my question.