Why PyAutoGui LocateOnScreen() only Returns None

44,993

Solution 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

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

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

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

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.

Share:
44,993

Related videos on Youtube

Raheem
Author by

Raheem

Updated on February 15, 2022

Comments

  • Raheem
    Raheem 10 months

    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
    
  • Sami Ahmed Siddiqui
    Sami Ahmed Siddiqui over 5 years
    Please make sure you format your code next time using the editor tools. Backticks don't work for code blocks.
  • Mihai Marinescu
    Mihai Marinescu over 5 years
    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
  • Bentaye
    Bentaye almost 5 years
    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
  • feltersnach
    feltersnach over 4 years
    @aroy you need to make grayscale = True and it will work.
  • Mario Uvera
    Mario Uvera over 1 year
    This worked for me using Windows 10 Python version 3.9.6. actually it hangs a bit after using "While"
  • Vincent Wen
    Vincent Wen over 1 year
    This answer resolve my problem, thanks a lot!
  • Naeem Khan
    Naeem Khan 10 months
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Related