'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


Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

I had the similar problem.

My fault was I had saved the compare picture as jpg first and then as png in MS paint.

Be sure to save the compare picture as png format. After this the Locate function worked for me.

Solution 4:[4]

I had same issue and kept returning None value.

I did several trials and found the solution for me. OS : MacOS

I saved photo with my system screenshot tool ( command+shift+5) and saved. it seems it's different pixel info as what it's displayed in my screen. Therefore I used pyautogui screenshot instead to save the photo which I wanted to.

pyautogui.screenshot('num7_.png', region=(260,360, 110, 100))

After that, it's working good regardless of grayscale parameter.

pyautogui.locateOnScreen('num7_.png')
Box(left=260, top=360, width=110, height=100)

Solution 5:[5]

The locateOnScreen() function returns None if the image wasn't found on the screen. Remember, the match has to be pixel-perfect in order to match it, so be sure to crop index.png to the smallest recognizable size to prevent extra details from ruining your match. Also, make sure the thing you are looking for is not obscured by any other windows on top of it.

Solution 6:[6]

I got this working by using the following:

r = None
while r is None:
    r = pyautogui.locateOnScreen('rbin.PNG', grayscale = True)
print icon_to_click + ' now loaded'

The key is to make grayscale = True.

Solution 7:[7]

The official documentation says;

The Locate Functions
NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, 
they’ll raise ImageNotFoundException instead of returning None.

So you can decide whether an exception was raise or not. Also you should try for a finite number of times not a While True loop.

retry_counter = 0
while retry_counter < 5:
    try:
        result = pyautogui.locateOnScreen(IMAGE_PATH_TO_FIND)
        if result:
            time.sleep(1)
            retry_counter = 10  # to break the loop
    except:
        time.sleep(1)  # retry after some time, i.e. 1 sec
        retry_counter += 1

Solution 8:[8]

I found that if you program a pause of 1 or 2 seconds (using time.sleep), then it is able to locate the image. It also takes time for python to locate the image (my computer took about 5 seconds).

Solution 9:[9]

I had that problem but then i crop the photo into specific part then it was locating and yes it takes time.

or this can also work.

b = pyautogui.center('calc7key.png')

Solution 10:[10]

I found a way to fix my problem. Only search for an image as small as possible. A picture that is only 1 pixel is found after 3 seconds. And when i try to search for an image over 500x500 then it won't find anything.

Solution 11:[11]

I think that the library pyautogui needs several recognition points. For example, Number seven on calculator:

enter image description here

The number seven on the calculator of windows 10. In this format, I´ve got the location on the screen. Thank you for your comments

Solution 12:[12]

My problem was I was trying to take a snip of the calculator button. That must make a different pixel match because I tried every other option in here and nothing was working. I did a print screen then cropped it to the button I wanted and it worked.

Solution 13:[13]

Before

import pyautogui
image = '9.png'
loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)
print (loc)

Error:

None
>>>

Solution

import pyautogui
import time
time.sleep(5)
image = '9.png'
loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)
print (loc)

Summary: Just add this two lines:

import time
time.sleep(5)

Output

Box(left=1686, top=248, width=70, height=47)
>>> 
import time
time.sleep(5)

Solution 14:[14]

If you have taken screenshot with snipping tool it wont work, so take screenshot with "prt sc" or command prompt. This worked for me!