How can I locate something on my screen quickly in Python?

Darth Vador picture Darth Vador · Mar 23, 2017 · Viewed 7.5k times · Source

I've tried using the pyautogui module and i's function that locates an image on the screen

pyautogui.locateOnScreen()

but it's processing time is about 5-10 seconds. Is there any other way for me to locate an image on the screen more quickly? Basically, I want a faster version of locateOnScreen().

Answer

Trilarion picture Trilarion · Mar 23, 2017

The official documentation says it should take 1-2 seconds on a 1920x1080 screen, so your time seems to be a bit slow. I would try to optimize:

  • Use grayscaling unless color information is important (grayscale=True is supposed to give 30%-ish speedup)
  • Use a smaller image to locate (like only a part if this is already uniquely identifying the position you need to get)
  • Don't load the image you need to locate from file everytime new but keep it in memory
  • Pass in a region argument if you already know something about the possible locations (e.g. from previous runs)

This is all described in the documentation linked above.

Is this is still not fast enough you can check the sources of pyautogui, see that locate on screen uses a specific algorithm (Knuth-Morris-Pratt search algorithm) implemented in Python. So implementing this part in C, may result in quite a pronounced speedup.