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.22k stars 1.24k forks source link

Inaccurate positioning #735

Open samshum opened 2 years ago

samshum commented 2 years ago

OS: MACOS Monterey12.6 Python version: Python 3.8's Code:

def testClickEvent(): btpoing = pyautogui.locateOnScreen('bt.png') x, y = pyautogui.center(btpoing)

pyautogui.click(x, y)

print(x, y)
print('-----------------------------')
while(True):
    print(pyautogui.position())
    time.sleep(1)

Result value: 4480 1712 Accurate value: Point(x=2267, y=855)

4480 1712

Point(x=1420, y=325) Point(x=1420, y=325) Point(x=1420, y=325) Point(x=1292, y=234) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855)

lemontreefour commented 1 year ago

Same issue with python 3.10.6 on Monterey and looks like returned coordinates are doubled somehow. Try dividing by 2. It worked for me. Did not ditched the code to fix it though but appreciate if someone can do it for the next release.

romaklym commented 1 year ago

OS: MACOS Monterey12.6 Python version: Python 3.8's Code:

def testClickEvent(): btpoing = pyautogui.locateOnScreen('bt.png') x, y = pyautogui.center(btpoing) #pyautogui.click(x, y) print(x, y) print('-----------------------------') while(True): print(pyautogui.position()) time.sleep(1)

Result value: 4480 1712 Accurate value: Point(x=2267, y=855)

4480 1712

Point(x=1420, y=325) Point(x=1420, y=325) Point(x=1420, y=325) Point(x=1292, y=234) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855) Point(x=2267, y=855)

I think it's a known issue. You most likely have a retina screen, which increases resolution (2x) of the amount of pixels which are on the screen. As @lemontreefour said, the easiest way to avoid this issue, is by dividing your returned position by 2.