why Python win32api.GetCursorPos() compress coordinates 1.25 times

46 Views Asked by At

For a long time I could not understand why the coordinates that I receive from win32gui.GetCursorPos() and those that I send to the screenshot do not match, until I noticed that for some reason they are compressed by 1.25 times . I attach the code below

import mss
import win32api
import win32gui
import time


def get_shift_state():
    last = None
    curr = None
    pos_array = []
    while True:
        curr = win32api.GetKeyState(160) # If shift is pressed
        if curr < 0 and curr != last:
            pos = win32gui.GetCursorPos()
            print(pos)
            pos_array.append([int(pos[0] * 1.25), int(pos[1] * 1.25)]) # Here is the Problem
            if len(pos_array) is 2:
                break
            # time.sleep(sec)

        last = curr
    print(pos_array)
    return pos_array


def take_screenshot(x0, y0, width, height, output='out.png'):
    with mss.mss() as sct:
        # Part of the screen to capture
        monitor = {"top": y0, "left": x0, "width": width, "height": height}
        while "Screen capturing":
            sct_img = sct.grab(monitor)
            mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
            break


def main():
    pos_array = get_shift_state()

    x0 = pos_array[0][0]
    y0 = pos_array[0][1]
    x1 = pos_array[1][0]
    y1 = pos_array[1][1]
    take_screenshot(x0, y0, x1 - x0, y1 - y0)


if __name__ == '__main__':
    main()

The program selects an area with two Shift clicks and saves a screenshot of this area in out.png

Python 3.11 pywin32 v306

the fix was:

pos_array.append([int(pos[0] * 1.25), int(pos[1] * 1.25)]) # * 1.25
0

There are 0 best solutions below