屏幕截图功能

PyAutoGUI可以截取屏幕截图,将其保存为文件,并在屏幕上定位图像。如果您有一个小图像(例如需要点击的按钮)并希望在屏幕上找到它,这将非常有用。这些功能由PyScreeze模块提供,该模块随PyAutoGUI一起安装。

截图功能需要Pillow模块支持。OS X系统使用操作系统自带的screencapture命令。Linux系统使用scrot命令,可通过运行sudo apt-get install scrot进行安装。

screenshot() 函数

调用screenshot()将返回一个Image对象(详情请参阅Pillow或PIL模块文档)。传入文件名字符串将把屏幕截图保存为文件,同时仍以Image对象形式返回。

>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im2 = pyautogui.screenshot('my_screenshot.png')

在1920x1080分辨率的屏幕上,screenshot()函数大约需要100毫秒——不算快但也不算慢。

还有一个可选的region关键字参数,如果您不需要截取整个屏幕。您可以传入一个包含左、上、宽度和高度四个整数的元组来指定要捕获的区域:

>>> import pyautogui
>>> im = pyautogui.screenshot(region=(0,0, 300, 400))

定位函数

注意:从0.9.41版本开始,如果定位函数无法找到提供的图像,它们将抛出ImageNotFoundException异常,而不是返回None

如果您拥有某个元素的图像文件,就可以在屏幕上直观地定位它。例如,假设计算器应用程序正在您的计算机上运行,看起来像这样:

_images/calculator.png

如果你不知道计算器按钮在屏幕上的精确坐标,就无法调用moveTo()click()函数。每次启动计算器时,它可能出现的位置略有不同,导致你每次都需要重新查找坐标。不过,如果你有按钮的图像,比如数字7按钮的图像:

_images/calc7key.png

...你可以调用locateOnScreen('calc7key.png')函数来获取屏幕坐标。返回值是一个包含4个整数的元组:(left, top, width, height)。这个元组可以传递给center()来获取该区域中心的X和Y坐标。如果在屏幕上找不到图像,locateOnScreen()会抛出ImageNotFoundException异常。

>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png')
>>> button7location
Box(left=1416, top=562, width=50, height=41)
>>> button7location[0]
1416
>>> button7location.left
1416
>>> button7point = pyautogui.center(button7location)
>>> button7point
Point(x=1441, y=582)
>>> button7point[0]
1441
>>> button7point.x
1441
>>> button7x, button7y = button7point
>>> pyautogui.click(button7x, button7y)  # clicks the center of where the 7 button was found
>>> pyautogui.click('calc7key.png') # a shortcut version to click on the center of where the 7 button was found

可选的 confidence 关键字参数指定了函数在屏幕上定位图像的精确度。这在函数由于细微像素差异而无法定位图像时特别有用:

>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9)
>>> button7location
Box(left=1416, top=562, width=50, height=41)

注意:您需要安装OpenCV才能使confidence参数生效。

locateCenterOnScreen() 函数结合了 locateOnScreen()center() 的功能:

>>> import pyautogui
>>> x, y = pyautogui.locateCenterOnScreen('calc7key.png')
>>> pyautogui.click(x, y)

在1920x1080分辨率的屏幕上,locate函数调用通常需要1到2秒。对于动作类电子游戏来说这可能太慢了,但对于大多数用途和应用来说已经足够。

pyautogui提供了多个"定位"功能函数。这些函数都会从屏幕(或图像)的左上角开始查找,先向右扫描,再向下扫描。参数可以是

  • locateOnScreen(image, grayscale=False) - 返回屏幕上首次找到的image实例的(left, top, width, height)坐标。如果在屏幕上未找到,则抛出ImageNotFoundException异常。
  • locateCenterOnScreen(image, grayscale=False) - 返回屏幕上找到的第一个image实例中心点的(x, y)坐标。如果在屏幕上未找到,则抛出ImageNotFoundException异常。
  • locateAllOnScreen(image, grayscale=False) - 返回一个生成器,该生成器会生成屏幕上找到图像位置的(left, top, width, height)元组。
  • locate(needleImage, haystackImage, grayscale=False) - 返回在haystackImage中找到的第一个needleImage实例的(left, top, width, height)坐标。如果在屏幕上未找到,则抛出ImageNotFoundException异常。
  • locateAll(needleImage, haystackImage, grayscale=False) - 返回一个生成器,该生成器会生成(left, top, width, height)元组,表示在haystackImage中找到needleImage的位置。

"定位所有"功能可用于for循环或传递给list()

>>> import pyautogui
>>> for pos in pyautogui.locateAllOnScreen('someButton.png')
...   print(pos)
...
(1101, 252, 50, 50)
(59, 481, 50, 50)
(1395, 640, 50, 50)
(1838, 676, 50, 50)
>>> list(pyautogui.locateAllOnScreen('someButton.png'))
[(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]

这些"定位"功能相当耗费资源;可能需要整整一秒才能运行完成。加速它们的最佳方式是传入region参数(一个由左、上、宽、高组成的4整数元组),这样只需搜索屏幕的较小区域而非整个屏幕:

>>> import pyautogui
>>> pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400))

灰度匹配

可选地,您可以在定位函数中传入grayscale=True参数以获得轻微的速度提升(约30%左右)。这会使图像和屏幕截图去饱和度,从而加快定位速度,但可能导致误匹配。

>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True)
>>> button7location
(1416, 562, 50, 41)

像素匹配

要获取屏幕截图中某个像素的RGB颜色值,可使用Image对象的getpixel()方法:

>>> import pyautogui
>>> im = pyautogui.screenshot()
>>> im.getpixel((100, 200))
(130, 135, 144)

或者作为一个单独函数,调用pixel() PyAutoGUI函数,这是对之前调用的封装:

>>> import pyautogui
>>> pix = pyautogui.pixel(100, 200)
>>> pix
RGB(red=130, green=135, blue=144)
>>> pix[0]
130
>>> pix.red
130

如果您只需要验证单个像素是否与给定像素匹配,可以调用pixelMatchesColor()函数,传入X坐标、Y坐标以及代表颜色的RGB元组:

>>> import pyautogui
>>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyautogui.pixelMatchesColor(100, 200, (0, 0, 0))
False

可选的 tolerance 关键字参数指定了红、绿、蓝各颜色值在匹配时允许的偏差范围:

>>> import pyautogui
>>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134))
False
>>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
True