Python: How can I find an image on screen by using: pyautogui lib?

george picture george · Oct 2, 2015 · Viewed 20k times · Source

The code is:

import pyautogui
startButton = pyautogui.locateOnScreen('start.png')
print startButton

Or:

import pyautogui
startButton = pyautogui.locateCenterOnScreen('start.png')
print startButton

The output is:

None

Note: the correct syntax seems to be in place according to the documentation.

Note: I have tried also with image full path. The image is on the screen and it is not overlapped by other images. The pil library is also installed. Other pyautogui features work (including taking screenshot)

Please let me know what I am missing out. Or please suggest another Python library for image detection.

Answer

Malachi Bazar picture Malachi Bazar · Apr 15, 2016

Here is the syntax I use for this:

import pyautogui
start = pyautogui.locateCenterOnScreen('start.png')#If the file is not a png file it will not work
print(start)
pyautogui.moveTo(start)#Moves the mouse to the coordinates of the image

If you are using multiple monitors at the same time it only scans the primary one.

This program scans the pixels of your screen and color matches pixels with your PNG file. If the image color(shadows of the image, the image is changing colors, etc.) changes in any way it will reply with "None".