Set tolerance in click(image)

Asked by Max on 2017-12-11


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"):
                    exit = 1

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

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
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.

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)

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))

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):
        type("t", KeyModifier.CTRL)

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

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:
        type("t", KeyModifier.CTRL)

BTW: already know the docs

while true
    if exists(Pattern("1513067174539.png").similar(0.5)):
       # click(getLastMatch()) #with versions before 1.1.1
        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:
see faq 1437

Max (max883) said : #5

Thanks RaiMan, that solved my question.