Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 682
george Avatar asked Oct 02 '15 12:10

george


People also ask

How does Pyautogui locate work?

PyAutoGUI can take screenshots, save them to files, and locate images within the screen. This is useful if you have a small image of, say, a button that needs to be clicked and want to locate it on the screen. These features are provided by the PyScreeze module, which is installed with PyAutoGUI.

Can Pyautogui be detected?

No, as far as I know, there is no way for a website to detect the device or software (such as PyAutoGUI) which is making inputs, however, a site could detect robotic mouse movement etc., and you will not be able to pass CAPTCHAs.


2 Answers

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".

like image 141
Malachi Bazar Avatar answered Oct 19 '22 20:10

Malachi Bazar


None means that PyAutoGui was unable to find your image on the screen, make sure the window is open where Start.png was sampled from and that the resolutions [from when you took the sample and the current monitor] are the same.

like image 21
AP. Avatar answered Oct 19 '22 20:10

AP.