21
import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))

Instead of returning coordinates, it returns None.

GLHF
  • 3,835
  • 10
  • 38
  • 83
Venkatesh J
  • 211
  • 1
  • 2
  • 7
  • @GLHF What do you mean by "The pictures are on my desktop"? This code will look for the actual image if it is visible on the screen. If it doesn't find it, it will return `None` as per [Don's answer](http://stackoverflow.com/a/37040311/838992) and you'll get this error message. Note the image has to be visible to the user at the point at which the code is running. It won't find the icon on your desktop, or similar, or the image in a window that is "hidden" behind another. Is that what you're trying to do? – J Richard Snape May 09 '16 at 11:30
  • I get the same error. Tried with a print screen image and the actual downloaded image. Tried the example from http://pyautogui.readthedocs.io/en/latest/screenshot.html with the calculator. I only get the response: TypeError: 'NoneType' object is not iterable. Working on windows 10 – Mihai Marinescu May 22 '17 at 14:22

9 Answers9

11

My problem is Solved when I took screenshot by pyautogui inbuilt function rather than taking WIN+Printscr because if we took screenshot by WIN+Printscr then pixel density and other image related data may be different in comparison to pyautogui inbuilt function.
Maybe this thing worked for you, for me it worked.
For Ex - wifi.png wifi.png so first I took full screenshot and I cropped it from that full image then I put this in my code shown below

import pyautogui
print(pyautogui.locateCenterOnScreen('wifi.png'))
Zephyr
  • 11,891
  • 53
  • 45
  • 80
abhishek Singh
  • 332
  • 2
  • 7
7

Seems like it couldn't find anything matching your image on the screen.

locateCenterOnScreen(image, grayscale=False) - Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Returns None if not found on the screen.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • 1
    Don't want to go stealing the bounty, this answer is more or less right - I think you should maybe add, though, it matters whether the image is **visible** in a windowed environment. So if an image matching the specified target image is "on screen" in that it is in a window, but that window is obscured by being "behind" another, `None` will be returned. Also - the bounty award says "the pictures are on my desktop". I wonder if this means that the file icons are on the desktop rather than the actual images are displayed - possible misunderstanding... – J Richard Snape May 05 '16 at 09:40
2

The initial problem is quite simple - the library does not find the image passed represented on the screen and therefore returns None rather than the co-ordinates as it says it will in the docs.

However, there is a possible misunderstanding here, in particular from a user who posted a bounty on the question and posed a similar question here.. A comment was made

"The pictures are on my desktop"

When you use this function, you pass in a filename as a string. The library then loads the image file and looks for the picture on screen (not the filename). pyautogui.locatecentreonscreen() will look for the actual image if it is visible on the screen. It does not look for files on the desktop, or file icons with the same name as the image passed to it.

Example

Say you have a file with the name flower.jpg containing the following image, saved on your desktop.

enter image description here

With no other windows open, run:

coords = pyautogui.locateCenterOnScreen('C:\\Richard\\Users\\flower.jpg')
print(coords)

The result is None

This is because that image is not displayed on my screen even though an icon is on the desktop, with the name flower.jpg. This is true even if that icon is a small scale version of the flower.

However, if I leave the image visible (as I'm preparing this post) and do the same thing, I get co-ordinates - e.g.:

enter image description here

As you see - because the actual image is on the screen, the library finds it, with co-ordinates 524,621

In summary if the library doesn't find the image displayed to the user on the screen, it will return None. Note the image has to be visible to the user at the point at which the code is running. It won't find the icon on your desktop, or similar, or the image in a window that is "hidden" behind another. Is that what you're trying to do?

Community
  • 1
  • 1
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • Good explanation, is there any library that work better than pyautogui? I mean it wants `excatly` the same picture on the screen. We need `similar` sometimes. – GLHF May 11 '16 at 15:45
  • 1
    @GLHF Under the hood, pyautogui uses a module by the same author `pyscreeze` - [code here](https://github.com/asweigart/pyscreeze/blob/master/pyscreeze/__init__.py) - for the locate functions. Looking at the code there, it must be an exact match, not a scaled version, or "similar". I am not aware of a similar library doing fuzzy, or "similar" matches, but you could maybe do it in two steps. One option would be to take a screenshot (e.g. using `autopygui`) and then use `openCV` to do the matching - http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html – J Richard Snape May 11 '16 at 16:11
1

Are you sure that the image is of the same size as of the icon?

If not pyautogui.locateCenterOnScreen() will raise TypeError: 'NoneType' object is not iterable

Also make sure that the full icon is visible and looks the same as the image:"C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"

Hope the problem is solved!

Vin
  • 729
  • 9
  • 15
  • `pyautogui.locateCenterOnScreen()` is also affected if you change your screen size in your system settings. Example: if you took the screenshot using a monitor, *then*, compare it to your screen when you use a different monitor or a laptop. The original screen size has to be the exact same in most cases with `auto resizing` on browsers, etc. So, yeah, this got me a few times at first too. – JayRizzo Aug 01 '19 at 03:33
0

Building off of what Don Kirby said, no matching image was found on the screen. You could open the image in, for example, Windows Photo Gallery, (or Tk) and then pyautogui would find it.

NoOneIsHere
  • 1,054
  • 16
  • 28
0

Good explanation, is there any library that work better than pyautogui? I mean it wants excatly the same picture on the screen. We need similar sometimes. – GLHF May 11 '16 at 15:45

Try using this code line:

pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)

I believe confidence range from 0.1-0.9. Unless you have several pictures looking almost alike, this might solve the exception. If that doesn't work try making a second screenshot with more/less of the original image and write this code:

try:
    pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
except TypeError:
    pyautogui.locateCenterOnScreen("yourscreenshot2.PNG", confidence=0.9)

This will give it a second try with a slightly different picture, and hopefully not return a TypeError.

Aleks
  • 1
0

If you can't use pyautogui.locateCenterOnScreen() because of image problem , try using the snipping tool (if you are on Windows) to take screenshots.It works. Also make sure that you have downloaded the "Pillow" module

0

Try this :

pip install opencv-contrib-python

It confused me a lot that I ran the same code:

coords =pyautogui.locateCenterOnScreen('C:\\test.jpg')

in two different virtual environment( X and Y, almost same) returned None and Point(x=1543, y=461).

I read Aleks's answer and guess it use the parameter confidence implicitly when opencv-contrib-python in current environment(which Y had but X hadn't).

I didn't dig in but just installed opencv-contrib-python in virtual environment X and solved my problem.

Zzh1996
  • 11
  • 1
0

there is no need to to take a screenshot you can tweek confidence parameter by default it it only returns if image has 100% match. What you can do is

pic_location = pyautogui.locateOnScreen("edge-pic.png",confidence=0.9)

but for that you need to import opencv-python package use command.

pip install opencv-python
  • 0.9 means a 90% match – Taha Irtaza Aug 28 '23 at 17:23
  • 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 Aug 30 '23 at 23:37