问题描述
我有 2 个坐标(x,y),我想将它们相减
你可以忽略它在pyautogui中,我如何减去test1-test2
import pyautogui
test1 = pyautogui.locateCenterOnScreen("test1.png",confidence=0.8)
test2 = pyautogui.locateCenterOnScreen("test2.png",confidence=0.9)
print(test1)
print(test2)
>>>Point(x=1072,y=543)
>>>Point(x=1304,y=689)
解决方法
The pyautogui
documentation 表明 test1
和 test2
值是 Python 元组,而不是 Point
对象:
locateCenterOnScreen()
函数只返回图像在屏幕上的中间位置的 XY 坐标:
>>> pyautogui.locateCenterOnScreen('looksLikeThis.png') # returns center x and y
(898,423)
对于access elements of a tuple,您可以使用[0]
、[1]
等来获取第一个、第二个和后续元素。
要计算两个元组的差异,您可以use map
对来自test1
和test2
的每对元素运行anonymous function:
diff = tuple(map(lambda i,j: i - j,test1,test2))
注意元组的顺序会改变结果。 test1 - test2
的结果将与 test2 - test1
不同。因此,您可能想要absolute value 的差异:
abs_diff = tuple(map(lambda i,j: abs(i - j),test2))
,
我认为您可以将 Point 对象转换为列表
test1 = list(test1)
然后访问 X 或 Y 坐标:
print(f"X coordinate: {test1.x}")
print(f"Y coordinate: {test1.y}")