True or False when image is visible

Asked by Calle Rundgren

I want to check the screen if a certain image is visible. If the image is visible i want to return True and if it is not visible I want to return False.

Is this the proper solution or is it any better solution?

try:
    exists(image)
    return True
except: return False

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

Since you are using return, I suppose we are talking about a function.

try this:

def myCheck(image):
    if exists(image): return True
    return False

try:/except: cannot be used, since exists() does not throw exceptions

same using find():

def myCheck(image):
    try:
        find(image)
        return True
    except:
        return False

But why are you not using exists() directly? it returns a Match object if found and None if not. This can be used in any context where True/False are valid (if/elif/else, while).
So what is your intention/usage?

Revision history for this message
Calle Rundgren (c-rundgren) said :
#2

Yes we are talking about a function.

I just tested both of your above solutions and both are working. From what I have been reading in other examples exists() is to prefer above find()?

I have no answer for why I am not using exists directly, Maybe I was to locked up in mind that I got to use an if-statement. Or that there are supposed to be some other tasks before the function and therefore I wanted to use if exists().

There are two possible intentions.
1. The usage is to check if a certain image is visible at a certain moment, and then return either True or False. In order to be able to add more code to the function which perform different tasks depending on if True or False is returned. Example on which code who could be added:

while True:
    App.open(r"c:\example.exe")
    break

Or is it better to use while directly like this:

while exists(image):
    App.open(r"c:\example.exe")

2. There are many functions after eachother so another intention would be to know which function who returned True/False.

Revision history for this message
Calle Rundgren (c-rundgren) said :
#3

If I am using try: in order to check if images can be found or not.
def myFunc():
    try:
        find(image1)
    except:
        return FindFailed

If i have multiple images which shall be searched after eachother, is it possible to put them all in an array in order to loop through it and raise an exception if there is any?

Or if I do like this:

try:
    find(image1)
except:
    return FindFailed
try:
    find(image2)
except:
    return FindFailed
try:
    find(image3)
except:
    return FindFailed

Would i need to have this many try: to search for multiple images? Or is this enough:

try:
    find(image1)
    find(image2)
    find(image3)
except:
    FindFailed

From what I think this above operation would just raise one exception when the first image is not found? What happens if there for instance is two images which can not be found? Would two exceptions be raised or do I have to use the code where one exception is raised per image not found?

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

*** on comment #3:

This is exactly, why I always recommend to use exists() instead of find() when dealing with decisions (image there or not).

So clearly: use exists() for these purposes!

... and if you have a series of checks, use exists(image, 0), to make it as fast as possible.

--- so your example (in a generic way):

def myFunc(imagelist):
    result = False
    for img in imageList:
        if exists(img, 0): result = True
    return result

usage:

if myFunc((image1, image2, image3)): print "at least one Found"
else: print ("None found")

--- or a version, that tells you, which where found:

def myFunc(imagelist):
    result = []
    for img in imageList():
        if exists(img, 0): result.append(getLastMatch)
        else: result.append(None)
    return result

usage:

images = (image1, image2, image3)
found = myFunc(images)
if not found: print ("None found")
else:
     print ("found the following")
     for n in range(len(found)):
         if found[n]: print images[n]+" found at: "+str(found[n])

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

*** on comment #2

--- first example:

while True:
    App.open(r"c:\example.exe")
    break

is the same as:

App.open(r"c:\example.exe")

--- second example:

while exists(image):
    App.open(r"c:\example.exe")

would loop as long as image is found and each time process App.open().

so if you want it only once:

if exists(image): App.open(r"c:\example.exe")

Revision history for this message
Calle Rundgren (c-rundgren) said :
#6

Thanks RaiMan, that solved my question.

Revision history for this message
Calle Rundgren (c-rundgren) said :
#7

Thank you RaiMan! You are an angel sent from heaven! :-)