yangd01234
yangd01234

Reputation: 251

Fastest Way to Check if Image is Onscreen Using pyautogui

I have the following code:

b_counter = False

while (b_counter == False):
    if (pyautogui.locateOnScreen('editcheck.png') is not None):
            pyautogui.click(pyautogui.center(pyautogui.locateOnScreen('home.png')))
            b_counter = True

The loop lags when running, sometimes up to 10 seconds after the image has loaded. My machine itself is pretty fast (i7 16gb ram etc). Is there any way to speed up this loop?

I'm loading webpages so the "home" buttons do not appear immediately. This loop occurs after pyautogui has entered in the web address to load.

Upvotes: 1

Views: 10023

Answers (2)

PyBoss
PyBoss

Reputation: 631

simple way

if (pyautogui.locateOnScreen('test.PNG') is not None):
    pyautogui.hotkey('win', 'l')

Upvotes: 2

yangd01234
yangd01234

Reputation: 251

Found an answer to my question. Select a region to make the search feature faster. This way, the program won't be searching through the whole screen, just a specific region.

ex:

    import pyauotgui
    pyautogui.locateOnScreen('test.png', region = (0,0,400,400))

where region = left, top, width, height

Upvotes: 5

Related Questions