Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyautogui.locateCenterOnScreen() returns None instead of coordinates

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

Instead of returning coordinates, it returns None.

like image 395
Venkatesh J Avatar asked Aug 22 '15 12:08

Venkatesh J


People also ask

What does Pyautogui locateOnScreen return?

The Locate Functions … you can call the locateOnScreen('calc7key. png') function to get the screen coordinates. The return value is a 4-integer tuple: (left, top, width, height).

How do you get to the center of the screen in Pyautogui?

You can directly find the center of the image using the built in locateCenterOnScreen method. Returns (x, y) coordinates of the center of the first found instance of the image on the screen.


3 Answers

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'))
like image 86
abhishek Singh Avatar answered Oct 18 '22 13:10

abhishek Singh


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.

like image 36
Don Kirkby Avatar answered Oct 18 '22 14:10

Don Kirkby


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?

like image 34
J Richard Snape Avatar answered Oct 18 '22 14:10

J Richard Snape