首页 > 解决方案 > 是否有一个公式可以在 Python 中为分辨率创建通用坐标?

问题描述

在 autoit 中,代码如下:

Func _ConvertXY(ByRef $Xin, ByRef $Yin)
    $Xin = Round(($Xin / 2560) * @DesktopWidth)
    $Yin = Round(($Yin / 1440) * @DesktopHeight)
EndFunc   ;==>_ConvertXY

$flashXa = 1059
$flashYa = 1285 ; Your intended coordinates on the original 2560x1440 desktop
_ConvertXY($flashXa, $flashYa) ; Convert proportionally to the actual desktop size

我正在尝试做同样的事情,但在 Python 中 - 所以基本上它会将坐标调整为相对于 1440p 屏幕的屏幕尺寸,而不管您的分辨率如何。

标签: pythonscreen

解决方案


以下是我收集到的您正在寻找的内容:

def res_conv(x: int, y: int, resolution_from: tuple, resolution_to: tuple) -> tuple:
    assert x < resolution_from[0] and y < resolution_from[1], "Input coordinate is larger than resolution"
    x_ratio = resolution_to[0] / resolution_from[0] # ratio to multiply x co-ord
    y_ratio = resolution_to[1] / resolution_from[1] # ratio to multiply y co-ord
    return int(x * x_ratio), int(y * y_ratio)

例子:

res_conv(100, 100, (2560, 1440), (1920, 1080)

将返回:

(75, 75) 

推荐阅读