asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.14k stars 1.23k forks source link

macOS - `locateOnScreen()` coordinates are off by a factor of 2 #589

Closed HeisenBug1 closed 3 years ago

HeisenBug1 commented 3 years ago

locateOnScreen() works perfectly on Ubuntu Linux but on MacOS it always adds a varying offset. As X increases, the coordinates returned by locateOnScreen() increases by a certain ratio, similar for Y but increases slightly less.

Doesn't matter how many desktop/workspaces I have active, the result is the same.

ClementDelannoySlateDigital commented 3 years ago

On my screens, there is a factor 2 on x and y returned by locateCenterOnScreen

HeisenBug1 commented 3 years ago

On my screens, there is a factor 2 on x and y returned by locateCenterOnScreen

that's correct. the coordinates are off by a factor of 2. so a workaround is to use locateCenterOnScreen() and divide the coordinates by 2 before using them.

ClementDelannoySlateDigital commented 3 years ago

I will reopen that because it still needs a fix even if a workaround is available

hangj commented 3 years ago

The factor 2 is devicePixelRatio.
pixelRatio = pyautogui.screenshot().size[0]/pyautogui.size().width

HeisenBug1 commented 3 years ago

@hangj thanks... instead of a fixed factor 2, a pixel ratio would be better..

beauxq commented 2 years ago

This is an issue with Windows also, with its scaling options.

With 125% scaling, which is the default and recommended on this machine, the coordinates of locateCenterOnScreen() are 1.25 times what they should be.

chrischma commented 1 year ago

very interesting!

caiooaragao commented 3 months ago

The factor 2 is devicePixelRatio. pixelRatio = pyautogui.screenshot().size[0]/pyautogui.size().width

can u explain more about it?

HeisenBug1 commented 3 months ago

The factor 2 is devicePixelRatio. pixelRatio = pyautogui.screenshot().size[0]/pyautogui.size().width

can u explain more about it?

Sometimes the screen is scaled up/down, either by default, or you can do it manually to make things bigger/smaller. This causes the LocateCenterOnScreen() or any other similar functions to be off by the factor the screen was scaled to. In my case it was a factor of 2. So instead if we use this formula to get a device pixel ratio, it'll work for every scaled screen sizes..