Want to use 2 images in if exists statement

Asked by Ankit

I have to images say A and B. Now I want to check if A or B exists then its true. I have tried various methods but it did not work out. Can anybody tell me how can I use 2 images in if exists statement?
Note: I am using RC1 version.

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

--- if you do not need to know, which one
if exists(imgA, 0) or exists(imgB, 0):
    print "either imgA or imgB is there"
else:
    print "not there"

--- if you want to know, which one:
isImgA = False
isImgB = False
if exists(imgA, 0): isImgA = True
if exists(imgB, 0): isImgB = True
if isImgA or isImgB:
    print "either imgA or imgB is there"
    if isImgA:
        print "imgA is there"
    if isImgB:
        print "imgB is there"
    if isImgA and isImgB:
        print "imgA and imgB are there"
else:
    print "not there"

--- comment on exists(img, 0)
0 as the second parameter to exists forces, that only one search is executed and the result returned immediately. It does not wait the standard 3 seconds, so it is very responsive. So this version is ideal for situations like you have, where you only want to know, wether an image is there or not, to make a decision on the further workflow.
To check 3 images "concurrently" costs about 1 - 2 seconds.

Revision history for this message
Ankit (ankit-kd123) said :
#2

Hi RaiMan,

I tried if exists(imgA, 0) or exists(imgB, 0): But I want to wait until 1 one of the 2 images appear.
So I used if exists(imgA, 10) or exists(imgB, 10): But through this statement the script waits for 20 seconds instead of 10.
So is there a way by which the script will wait for 10 seconds if 1 of the 2 images does not appear?

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

Ok, if this should do what you want:

max = 10.0 # max waiting time in seconds
pause = 0.0 # pause in seconds between search trials
start = time.time()
found = False
while time.time()-start < max:
    if exists(imgA, 0) or exists(imgB, 0):
        found = True
        break
    if pause > 0: wait(pause)
if found:
    print "either imgA or imgB is there"
else:
    print "not there"

If you need the information, which one appeared, you have to adapt the second above solution into the loop.

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

BTW: if you have more of these situations, it might be a good idea to check the observe feature (see docs)

One more thing: even more ideas and background:
faq 1568 and faq 1731

Revision history for this message
Ankit (ankit-kd123) said :
#5

Thanks RaiMan, that solved my question.