select areas

Asked by Danielo

Hello, y want to select an area of the screen and then work only on that area. The way i'm doing it now is this, but im not sure if is correct:

pic = capture() # this enters interactive mode, user captures and filename of result saved in pic
reg = find(pic)

 for x in reg.findAll("picture A" ):
    click(x)
    if not reg.exists("picture B" ):
        click("picture C" )

I want to do that because i want to improbe performance and i want to let te user select where he wants to put the window for the script to let him make other things. Thanks very much.

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
Best RaiMan (raimund-hocke) said :
#1

if you don't need the visual content of the selected area any more (I guess you don't) it is sufficient to say:

reg = selectRegion("Hi user, pls. select the area")

after that, 3 possibilities to restrict the searches to that region (which boosts performance):

--- 1---
supposing it is only a single screen system:
setRoi(reg)
# all subsequent unqualified find()'s and actions are directed to Screen(0) but restricted to the area specified by reg
downside: if you want to work on the whole screen again, you have to reset the ROI (region of interest):
setROI(SCREEN.getBounds())

--- 2 ---
qualify every action/find with reg.
In your current script the implicit find() with the click(picC) would still search the whole screen:
   if not reg.exists("picture B" ):
        reg.click("picture C" ) # if it is inside reg too

--- 3 ---
use a with reg: block, so all unqualified find()s and actions within the block go to the specified region

reg = selectRegion("Hi user, pls. select the area")

with reg:
  for x in findAll("picture A" ):
    click(x)
    if not exists("picture B" ):
        click("picture C" )

my oppinion:
2 and 3 are the most flexible, because the standard "unqualified means SCREEN (which is a constant reference to Screen(0)" is left as it is in the default.

Revision history for this message
Danielo (rdanielo) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Danielo (rdanielo) said :
#3

Thank you. I was using region.click, but i was not sure if it works because i have read (maybe in an old question) that it was not suported.

This is really what i was looking for. I think the 3 option is definitely the most powerful. It works like a loop true? Everything indented under with reg will happen only on that region, isn't it?

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

You say:
It works like a loop true? Everything indented under with reg will happen only on that region, isn't it?

I answer:
Yes, everything in the indented block happens only to reg, BUT it does not loop. if you need that, you have to add a while True: either outside or inside the with block depending on your intention.

with reg:
   while True:
      if exists(img,0): # searched for in reg
         break # end loop if found
      wait(1) # loop again
   find(img1) # searched for in reg after loop ends
find(img2) # searched for on the whole screen since outside with

Revision history for this message
Danielo (rdanielo) said :
#5

Thanks, you have explained it very well.

Of course i know it wil not loop, it was only one way to explain it.

Thank you very much, your examples are a lot of usefull and helps me a lot too.