pyautogui.locateCenterOnScreen() returns None instead of coordinates

谁说我不能喝 提交于 2019-12-08 15:48:51

问题


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

Instead of returning coordinates, it returns None.


回答1:


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.




回答2:


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



回答3:


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.

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

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?




回答4:


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!




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/32156019/pyautogui-locatecenteronscreen-returns-none-instead-of-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!