import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))
Instead of returning coordinates, it returns None
.
import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))
Instead of returning coordinates, it returns None
.
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
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:
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?
Seems like it couldn't find anything matching your image on the screen.
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 thenpyautogui
would find it.Are you sure that the image is of the same size as of the icon?
If not
pyautogui.locateCenterOnScreen()
will raiseTypeError: '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!
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