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

Try installing opencv & decrease confidence. That worked for me

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

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


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.


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