Skip to content

Screen Recognition (Locating Images on Screen)

Concept

You take a small screenshot of a button (template image), then search for it on screen.

Example

locate_image.py
import pyautogui
 
# button_pos = pyautogui.locateCenterOnScreen("button.png", confidence=0.9)
# if button_pos:
#     pyautogui.click(button_pos)
locate_image.py
import pyautogui
 
# button_pos = pyautogui.locateCenterOnScreen("button.png", confidence=0.9)
# if button_pos:
#     pyautogui.click(button_pos)

Notes

  • confidenceconfidence requires OpenCV support
  • matching can fail due to scaling/themes

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did