Skip to content
Advertisement

how to convert normalized coordinates to pixel coordinates?

what is the most effective way to translate from normalized coordinates (x = 0,1) (y = 0,1)

to pixel coordinates (x = 0, 1920) (x = 0, 1080)

or is there even a way to do it inside python?

i have no idea where to even start.

cause im trying to get coordinates from mediapipe’s pose detection module, then tracking my mouse cursor to it

but mediapipe uses normalized coordinates and all of the mouse manipulator modules use pixel coordinates

thanks, best regards

Advertisement

Answer

If I am understanding correctly, you are given two decimal numbers between 1 and 0 and asked to scale them to fit the screen.

For simplicity, let’s just focus on the x axis. You have a ratio that represents how far across the screen your mouse is. For instance, 0.75 means you are 75% of the way across the screen. In order to convert to pixel coordinates, just multiply this percentage by the screen width. The same method can be applied to the y axis, just use the screen height instead of the screen width.

test_coord = (0.5, 0.3)

SCREEN_DIMENSIONS = (1920, 1080)


def to_pixel_coords(relative_coords):
    return tuple(round(coord * dimension) for coord, dimension in zip(relative_coords, SCREEN_DIMENSIONS))


print(to_pixel_coords(test_coord)) # prints (960, 324)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement