Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PyAutoGui LocateOnScreen() only Returns None

Here's the code that I'm trying to run:

import pyautogui
r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
print r
like image 822
Raheem Avatar asked Apr 30 '17 03:04

Raheem


2 Answers

It has to be a pixel-perfect match in order to be found. To allow for any sort of deviance you can invoke a confidence parameter.

For example:

loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)

However, in order to use the confidence parameter you have to have opencv_python installed. This is easy to install with pip:

./python -m pip install opencv_python

After that is in place, you should be able to account for minor differences.

like image 147
Prelator2 Avatar answered Oct 12 '22 01:10

Prelator2


I was encountering the same problem, what I did is

import pyautogui 
r= None 
while r is None:
    r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
print r

I think its just because that it takes time to locate image. If you found a better solution share with me :)

like image 30
aroy Avatar answered Oct 11 '22 23:10

aroy