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?
#1
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/6814...-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/6814...-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
Reply
#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
#3
Thank you! I think you are correct! I just found this...

https://stackoverflow.com/questions/3937...onscreen-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:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help me in regards to pyAutoGUI andrewtuk 2 1,424 Jan-01-2023, 05:22 PM
Last Post: gradlon93
  PyAutoGUI issues with detection Bizzy_ 0 4,885 May-03-2021, 01:36 AM
Last Post: Bizzy_
  pyautogui attribuyes not found rfresh737 7 2,991 Apr-14-2021, 01:32 AM
Last Post: rfresh737
  Trying to make random image loop in Tk window using python Jediguy18 1 3,155 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