Search code examples
pythonimagepython-3.ximage-processingpyautogui

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


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().


Solution

  • The official documentation says that it should take 1-2 seconds on a 1920x1080 screen. Your time seems to be a bit slow. I would try optimizing the code by doing the following:

    • Use grayscale unless color information needs to be considered (including grayscale=True should improve speed by approximately 30%).
    • Use a smaller image for detection (only a part of the desired target is required to be unique and identify the target's position).
    • Don't load the identifier image from local storage when searching for the target, keep it in memory.
    • Pass in a region argument if you know or can predict its possible location (e.g. from previous runs).

    This is all described in the documentation linked above.

    If 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. Implementing this part in C may result in a quite pronounced speedup.