Python: How can I find an image on screen by using: pyautogui lib?
Question:
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.
Answers:
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
- The resolutions from when you took the sample and the current monitor are the same.
I had a very analogical problem. For me the solution was to use python 2.7 instead of 3.x. It is probably caused by less flexible functionality of python 3. I prefer to use it and pyautogui works fantastically. The pillow module (or commonly known as PIL) which must be installed when installing pyautogui, however seems to have less functionality working with python 3.
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”.
As I understand the problem can be fix by turning the image to RGB. The code will look something like this:
import pyautogui
from PIL import Image
im1=pyautogui.screenshot()
im2=pyautogui.screenshot("newone.png")
image.open("newone.png").convert("RGB").save("newone.png")
You need to put the whole path of the image in pyautogui.locateCenterOnScreen()
, like this:
pyautogui.locateCenterOnScreen('C:/Users/User/Desktop/start.png')
This worked for me. You might also want to add x_cord, y_cord =
in front of that command, so you can use it later.
Try installing opencv & decrease confidence. That worked for me
import pyautogui
startButton = pyautogui.locateOnScreen('start.png', confidence = 0.7)
print(startButton)
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.
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
- The resolutions from when you took the sample and the current monitor are the same.
I had a very analogical problem. For me the solution was to use python 2.7 instead of 3.x. It is probably caused by less flexible functionality of python 3. I prefer to use it and pyautogui works fantastically. The pillow module (or commonly known as PIL) which must be installed when installing pyautogui, however seems to have less functionality working with python 3.
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”.
As I understand the problem can be fix by turning the image to RGB. The code will look something like this:
import pyautogui
from PIL import Image
im1=pyautogui.screenshot()
im2=pyautogui.screenshot("newone.png")
image.open("newone.png").convert("RGB").save("newone.png")
You need to put the whole path of the image in pyautogui.locateCenterOnScreen()
, like this:
pyautogui.locateCenterOnScreen('C:/Users/User/Desktop/start.png')
This worked for me. You might also want to add x_cord, y_cord =
in front of that command, so you can use it later.
Try installing opencv & decrease confidence. That worked for me
import pyautogui
startButton = pyautogui.locateOnScreen('start.png', confidence = 0.7)
print(startButton)