Python Forum
How can I loop PyAutoGUI's locateCenterOnScreen until the image is found?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I loop PyAutoGUI's locateCenterOnScreen until the image is found?
#2
Looking at PyAutoGUI's source code the function locateCenterOnScreen uses pyscreeze
https://github.com/asweigart/pyautogui/b...__.py#L205 Wrote:
    @raisePyAutoGUIImageNotFoundException
    def locateCenterOnScreen(*args, **kwargs):
        return pyscreeze.locateCenterOnScreen(*args, **kwargs)

    locateCenterOnScreen.__doc__ = pyscreeze.locateCenterOnScreen.__doc__

Looking at pyscreeze's source code the function locateCenterOnScreen has a parameter minSearchTime - amount of time in seconds to repeat taking screenshots and trying to locate a match. The default of 0 performs a single search.
https://github.com/asweigart/pyscreeze/b...__.py#L409 Wrote:
def locateCenterOnScreen(image, **kwargs):
    """
    TODO
    """
    coords = locateOnScreen(image, **kwargs)
    if coords is None:
        return None
    else:
        return center(coords)

https://github.com/asweigart/pyscreeze/b...__.py#L363 Wrote:
def locateOnScreen(image, minSearchTime=0, **kwargs):
    """TODO - rewrite this
    minSearchTime - amount of time in seconds to repeat taking
    screenshots and trying to locate a match.  The default of 0 performs
    a single search.
    """
    start = time.time()
    while True:
        try:
            screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
            retVal = locate(image, screenshotIm, **kwargs)
            try:
                screenshotIm.fp.close()
            except AttributeError:
                # Screenshots on Windows won't have an fp since they came from
                # ImageGrab, not a file. Screenshots on Linux will have fp set
                # to None since the file has been unlinked
                pass
            if retVal or time.time() - start > minSearchTime:
                return retVal
        except ImageNotFoundException:
            if time.time() - start > minSearchTime:
                if USE_IMAGE_NOT_FOUND_EXCEPTION:
                    raise
                else:
                    return None

So it already has a looping mechanism built in, try giving a minSearchTime
Reply


Messages In This Thread
RE: How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - by Yoriz - Sep-26-2021, 09:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help me in regards to pyAutoGUI andrewtuk 2 1,605 Jan-01-2023, 05:22 PM
Last Post: gradlon93
  PyAutoGUI issues with detection Bizzy_ 0 5,138 May-03-2021, 01:36 AM
Last Post: Bizzy_
  pyautogui attribuyes not found rfresh737 7 3,302 Apr-14-2021, 01:32 AM
Last Post: rfresh737
  Trying to make random image loop in Tk window using python Jediguy18 1 3,342 Dec-30-2020, 04:56 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020