26

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
Soviut
  • 88,194
  • 49
  • 192
  • 260
Raheem
  • 482
  • 1
  • 4
  • 8

15 Answers15

42

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.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Prelator2
  • 521
  • 4
  • 4
8

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

Soviut
  • 88,194
  • 49
  • 192
  • 260
aroy
  • 452
  • 2
  • 10
  • Please make sure you format your code next time using the editor tools. Backticks don't work for code blocks. – Soviut May 04 '17 at 02:35
  • 2
    Doesn't work for me. I tried the following: 1. download the one image from here: https://automatetheboringstuff.com/chapter18/ 2. added the image in the same folder of the python code; 3. kept the window open and the console next to it; 4. waited and waited - the image isn't found. it returns none; working on windows 10 – Mihai Marinescu May 22 '17 at 15:14
  • @aroy you need to make grayscale = True and it will work. – feltersnach Jun 14 '18 at 15:21
  • This worked for me using Windows 10 Python version 3.9.6. actually it hangs a bit after using "While" – Mario Uvera Jul 14 '21 at 21:14
7

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)
newkoguy
  • 71
  • 1
  • 1
6

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.

user1738998
  • 61
  • 1
  • 2
3

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
Umair Bhatti
  • 117
  • 1
  • 4
2

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.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
2

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.

feltersnach
  • 406
  • 3
  • 20
1

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)
Mario Uvera
  • 851
  • 1
  • 7
  • 7
1

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

Marc
  • 11
  • 1
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31068121) – Naeem Khan Feb 20 '22 at 05:12
0

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')
Khushal sharma
  • 423
  • 5
  • 10
0

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.

Walter
  • 184
  • 2
  • 13
0

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

Bentaye
  • 9,403
  • 5
  • 32
  • 45
diego
  • 1
  • Please insert the images in the body of your answers instead of giving a link, this way if the link breaks, the answer can still be understood – Bentaye Mar 07 '18 at 15:57
0

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.

0

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

0

I am sure most of you guys know this, but if you forget to run your python script or IDE (wether that be Visual Studio, Python IDLE, etc.) as an administrator, then pyautogui will not be able to use the click command. It will still be able to move the mouse around, but it just won’t be able to click (this is true for all operating systems, they prevent any software that dose not have administrative privileges from clicking and i think it also prevents the software from using the keyboard but I am not sure. The OS dose this as a security measure so no software can do malicious things on your computer without your consent, like for instance: prompting the user to allow the software administrator access, and then taking control of the mouse and keyboard to click the “Yes” button for the user). If you’re using Linux or Mac then I am sure you know: you would have to use the “sudo” command. Hope this helps someone ;)

Ashton
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '22 at 17:40