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


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

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