PyScreeze 基本使用(2)
PyScreeze定位函数 Locate Functions
如果你有一个图像文件,你可以在屏幕上直观地定位它。可以调用locateOnScreen(‘calas7key.png’)
函数来获取计算器应用程序的7
按钮的屏幕坐标。返回值是一个4整数元组:( left, top, width, height)
。可以将这个元组传递给Center()
,以获得该区域中心的X
和Y
坐标。如果在屏幕上找不到图像,则locateOnScreen()
将返回None
。
>>> import pyscreeze
>>> button7location = pyscreeze.locateOnScreen('calc7key.png')
>>> button7location
(1416, 562, 50, 41)
>>> button7x, button7y = pyscreeze.center(button7location)
>>> button7x, button7y
(1441, 582)
>>> pyscreeze.click(button7x, button7y) # clicks the center of where the 7 button was found
LocateCenterOnScreen()
函数可能是最常使用的函数,直接返回找到图像的中心位置,即是locateOnScreen(‘filename’)
和Center()
的合并:
>>> import pyscreeze
>>> x, y = pyscreeze.locateCenterOnScreen('calc7key.png')
>>> pyscreeze.click(x, y)
在1920 x 1080
屏幕上,定位函数调用大约需要1或2秒。对于动作电子游戏来说,这可能太慢了,但是对于大多数用途和应用程序都是有效的。
如果速度很重要,请安装可选的OpenCV
库(pip install cv2
)。如果可用的话,locateAll
计算将使用它,并花费不到1毫秒的时间在全屏搜索中查找所有匹配项。(这不包括捕捉截图所需的时间。)