name of image passed to function

Asked by rblack

Hello,

how to get the name of an image passed to a function as an object ?

def WaitAndClick(image,time,doclick = 1):
    try:
        wait(image,time)
        if doclick==1:
            click(image)
            sleep(5)
    except:
        exit("no image found")

in the excpet i would like to pu the image name or entire object even ?

image.name is not working, nor any other attribute.

Question information

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

if we are talking about the basic Sikuli usage, image is an image filename as a string.

exit(image + " not found")

Revision history for this message
rblack (rblack) said :
#2

Thank you RaiMan for fast answer, but it doesn't work.

I send image to my functions (imported from separate sikuli file) like that:
http://postimg.org/image/7ca9b4dkp/

This visual passing is very helpfull and i would not like to pass image paths, because the readiblity of the code will drop dramatically.

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

Ok, if you want to do such more sophisticated things, you should be aware, what is behind the image thumbnails in the IDE

- In the first case it is indeed an image file name (no badges)
- in the second case the image was processed with Preview and it is a Pattern now (Pattern("someImage.png").similar())

to let it work in all cases:
exit(str(image) + " not found")

with version 1.1.0 you can see, what code is behind the images:
in the menu View, there is a switch: Show Thumbnails (ctrl/cmd + t)

Revision history for this message
rblack (rblack) said :
#4

Thank you RaiMan!

Yes, i want to make complex automated reports, when an image will be not found.
Info about Pattern object type helped!

The output from str(image) is:
P(1444234433282.png) S: 0.7 T: 103,2

But:
image.getFilename() # a Pattern method
outputs absolute path, which is more usefull (the image can be sent automatically to email thanks to that)
i.e.:
/home/user/sikuli/scripts/script.sikuli/1444234433282.png

I understand that, to access getFilename() i need to pass always Pattern object to function.
Can I force that somehow without manually setting each image target offset or similarity level?

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

rather easy to solve:

# image is either string or Pattern
imageName = image
if isinstance(image, Pattern):
    imageName = image.getFilename()
print "[error] image not found %s" % (imageName)"

Revision history for this message
rblack (rblack) said :
#6

thank you!

image name is different then image.getFilename() , which gets the full path. Fortunately i can handle that ;)