15

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
4
  • That happens when the image of index.png was not found on the current screen
    – Taku
    Apr 30, 2017 at 3:09
  • The image is visible
    – Raheem
    Apr 30, 2017 at 3:28
  • Can you post your index.png file and also an image file of a screenshot of your entire screen? Nov 7, 2017 at 23:28
  • @Raheem check out my solution below. Jun 1, 2018 at 15:14

14 Answers 14

27

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.

1
  • This answer resolve my problem, thanks a lot! Sep 6, 2021 at 14:43
6

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

4
  • Please make sure you format your code next time using the editor tools. Backticks don't work for code blocks.
    – Soviut
    May 4, 2017 at 2:35
  • 1
    Doesn't work for me. I tried the following: 1. download the one image from here: 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 May 22, 2017 at 15:14
  • @aroy you need to make grayscale = True and it will work. Jun 14, 2018 at 15:21
  • This worked for me using Windows 10 Python version 3.9.6. actually it hangs a bit after using "While" Jul 14, 2021 at 21:14
5

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.

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.

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.

2

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

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

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

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 7, 2018 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

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

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

1
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.