Python Forum
How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? (/thread-35062.html)



How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - Tiel - Sep-26-2021

I'm running Python 3.8.10 on Lubuntu 20.04 LTS.

How can I modify:
a, b = pyautogui.locateCenterOnScreen('/home/image01.png', confidence=0.6, region=(25,500,1700,570))
so that it loops until image01.png is found?

See, in my script, the following works approximately 90% of the time for me:
a, b = pyautogui.locateCenterOnScreen('/home/image01.png', confidence=0.6, region=(25,500,1700,570))

But about 10% of the time it fails resulting in:
TypeError: 'NoneType' object is not iterable

Based on https://stackoverflow.com/questions/68140109/why-cant-pyautogui-locate-my-image-although-the-code-seems-to-be-just-fine it seems like instead of running my code once, perhaps I should loop the following function until the image is detected. I don't know how to implement it, but once again based on https://stackoverflow.com/questions/68140109/why-cant-pyautogui-locate-my-image-although-the-code-seems-to-be-just-fine the following seems like it would be helpful...

    def detect_image(path, duration=0):
        while True:
            image_location = pyautogui.locateCenterOnScreen(path)
            if image_location:
                pyautogui.click(image_location[0], image_location[1], duration=duration)
                break



RE: How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - Yoriz - Sep-26-2021

Looking at PyAutoGUI's source code the function locateCenterOnScreen uses pyscreeze
https://github.com/asweigart/pyautogui/blob/master/pyautogui/__init__.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/blob/master/pyscreeze/__init__.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/blob/master/pyscreeze/__init__.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


RE: How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - Tiel - Sep-26-2021

Thank you! I think you are correct! I just found this...

https://stackoverflow.com/questions/39370253/what-is-the-goal-of-the-minsearchtime-argument-in-the-pyautogui-locateonscreen-m Wrote:It's useful when you want to wait some time for an image to appear. PyAutoGUI will keep making screenshots and searching for the image until the minSearchTime has passed. I got this from the source code: