How to Paste coordinates of an image?

Asked by Evan Bonser

I need sikuli to communicate with another app. So I'm trying to locate an image using find and trying to paste it's x/y location into an input field using paste but can't figure it out (yes I've tried reading the documentation but i can't spot it). Thanks in advance.
What I have so far
loc = find(capturedpicture).getCenter()
paste(pictureofinputfield,[loc].x)

I basically want to track that image as it moves around the screen, feeding its changing coordinates to another app i want to make in a different language.

Question information

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

the var loc is a Location object, that knows his x and y coordinates (which in your case is the center point of your found image).

you get the coordinates as integer values by loc.getX() / loc.getY() (python like) or loc.x / loc.y (java like).

paste needs a string as input. so you have to use str() (thats python) in your case:

paste(pictureofinputfield,str(loc.x))

happy tracking ;-)

Revision history for this message
Evan Bonser (evandevon) said :
#2

Hi again! Thanks for your previous adivce - worked like a charm!
The only problem is it's very slow finding the image. I thought if I searched for an area that was within 100 pixels either side of its last reported location that would dramatically increase its speed. This is the code so far...Don't know where to put the region

While exists(Image2Track):
   loc = getLastMatch()
   loc.getX()
   paste(InputFieldImage,str(loc.x))
paste(InputFieldImage,str("GONE"))

As a side question, how do you find the location of an image but if its dimensions have changed so that it's now bigger/smaller?

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

To be more flexible, I would do it this way:

while True:
   mTrack = exists(Image2Track, 0)
   if not mTrack:
      break
   paste(InputFieldImage, str(mTrack.getX())

I avoid using find()'s in a while condition, since you have to use getLastMatch() inside the loop - and you never know wether you need more find()'s inside or have to restrict to a region.

The 0 as 2nd parm with the exists() forces the find() to be processed only once. I use it always in cases, where I am sure the image is there. It helps to be more precise with the captured image and the min similarity.

speed and region:
exists() in your case always searches the whole screen. But as I understand, this is only necessary the first time. afterwards the next find() should be successful in an area around the last match.

1. you can look for the next instance around the last on:
   mTrack = mTrack.nearby(100).exists(Image2Track, 0)
you have to set mTrack for the first time before entering the loop:
mTrack = Screen(0)

2. you can redefine your region of interest (ROI) you are searching in (saves the intermediate internal creation of new regions)
reg = Screen(0) # before entering the loop

   mTrack = reg.exists(Image2Track, 0)

and then calculate the ROI for the next time.
   reg.setROI(mTrack.getX()-100, mTrack.getY()-100, mTrack.getW()+200, mTrack.getH()+200)

my preferred version: (though the version with nearby() is more readable)

reg = Screen(0)
while True:
   mTrack = reg.exists(Image2Track, 0)
   if not mTrack:
      break
   paste(InputFieldImage, str(mTrack.getX())
   reg.setROI(mTrack.getX()-100, mTrack.getY()-100, mTrack.getW()+200, mTrack.getH()+200)
paste(InputFieldImage,str("GONE"))

Your side question:
the find() in Sikuli currently is based on an exact comparison pixel by pixel. So versions of the same picture with different dimensions on the same screen resolution (like they can be produced using vector grafics or functions of image libraries or apps) will have a different pixel configuration compared to the original. In the consequence a find will fail.
Only in situations, where the same picture is shown on different screen resolutions (for the user the picture will be either smaller or larger) find() will still be successful.

Can you help with this problem?

Provide an answer of your own, or ask Evan Bonser for more information if necessary.

To post a message you must log in.