Can Find(<image> or <image>) be used, what would the syntax be?

Asked by joel hatzfeld on 2011-08-05

I'm a non developer attempting to follow someone else's script, and if it can be modified in a find(<image> or <image>) sort of way, it would be a great deal better for what I'm using, but I have no idea what the syntax would be.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

You have to make it on your own, since there is no such feature.

-- The easiest way: using sequential exists()
if exists(img1):
   #do something
elif exists(img2):
   # do something else
   print "neither img1 nor img2"

This depends on the sequence and img2 processing will not be done, if img1 is there. If img1 does not exist it will take 3 seconds until img2 will be searched.

--- most sophisticated
# each find action is only done once
# all images are found
# information about what images were found is available
# a max time for whole action is spacified
imgs = [img1, img2, img3]
t = 3 # waiting time
res = [False for img in imgs]

start = time.time()
while time.time()-start < t:
    for i in range(len(imgs)):
        if exists(imgs[i], 0):
            res[i] = True
    if True in res: break
print res

res contains the information, which of the images where found

so if you only want to know, that at least one of the images was there:

if True in res:
    # do something
    # none of the images where found

specific actions depending on a specific image:

if res[0]:
    # special action for image 1

*** comments
- when trying to understand scripts, it is a good idea to look into the docs in parallel
- you need some basic knowledge of Python language, if you want to do more complex things. Especially the indentation magic is always a hurdle for beginners.

Josh (joshtan11) said : #2

Hi RaiMan or fellow moderators or creators,

Not sure if you are still helping out but I have an important question that has been bugging me for weeks.

How do I compute the following:

WAIT 120 secs for EITHER image A or image B to appear

If A appear, do step 1 (which includes a number of steps)
If B appear, do step 2

There will not be a case where both or neither of them appear, I don't want to set a sleep timer as I want the program to be robust and snappy

RaiMan (raimund-hocke) said : #3

no need to wait weeks until asking such questions ;-)
TIP: you would get more possible responses, if you post your own question, and not dock to an existing one.

BTW: the concepts for your solution is already sketched in comment #1 and still valid:

the steps, to get the most effective solution:
- evaluate the smallest possible region, where you expect the 2 images to appear (one or two regions, does not matter, minimum size is the goal)
- make a wait loop
- use exists(img, 0) to check appearence

reg1 = #evaluate region for image1
reg2 = #evaluate region for image2
waitUntil = time.time() + 120
while time.time() < waitUntil:
    if reg1.exists(image1, 0):
        # do what is needed
    if reg2.exists(image2, 0):
        # do what is needed

Can you help with this problem?

Provide an answer of your own, or ask joel hatzfeld for more information if necessary.

To post a message you must log in.