pyautogui.locateCenterOnScreen() returns None inst

2019-06-14 23:48发布

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

Instead of returning coordinates, it returns None.

5条回答
再贱就再见
2楼-- · 2019-06-15 00:17

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?

查看更多
爷的心禁止访问
3楼-- · 2019-06-15 00:24

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.

查看更多
We Are One
4楼-- · 2019-06-15 00:24

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.

查看更多
Lonely孤独者°
5楼-- · 2019-06-15 00:25

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!

查看更多
手持菜刀,她持情操
6楼-- · 2019-06-15 00:30

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. May be 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'))
查看更多
登录 后发表回答