适用于 Python 2 和 3 的简单跨平台屏幕截图模块。
项目描述
PyScreez
PyScreez 是一个用于 Python 2 和 3 的简单、跨平台的屏幕截图模块。
关于
PyScreeeze 可以截取屏幕截图,将它们保存到文件中,并在屏幕中定位图像。如果您有一个需要单击的按钮的小图像并希望在屏幕上找到它,这将非常有用。
屏幕截图功能需要 Pillow 模块。OS X 使用操作系统自带的screencapture命令。Linux 使用scrot命令,可以通过运行sudo apt-get install scrot 进行安装。
关于 Ubuntu 的特别说明
不幸的是,Ubuntu 在安装 Pillow 方面似乎存在一些缺陷。Ubuntu 上开箱即用的 Pillow 不包含 PNG 和 JPEG 支持。以下链接有更多信息
screenshot() 函数
调用screenshot()将返回一个 Image 对象(有关详细信息,请参阅 Pillow 或 PIL 模块文档)。传递文件名字符串会将屏幕截图保存到文件中,并将其作为 Image 对象返回。
>>> import pyscreeze >>> im1 = pyscreeze.screenshot() >>> im2 = pyscreeze.screenshot('my_screenshot.png')
在 1920 x 1080 的屏幕上,screenshot()函数大约需要 100 毫秒 - 它不快但也不慢。
如果您不想要整个屏幕的屏幕截图,还有一个可选的region关键字参数。您可以传递区域的左、上、宽和高的四整数元组来捕获:
>>> import pyscreeze >>> im = pyscreeze.screenshot(region=(0,0, 300, 400))
定位函数
如果您有图像文件,您可以在屏幕上直观地找到某些内容。您可以调用locateOnScreen('calc7key.png')函数来获取计算器应用程序的 7 按钮的屏幕坐标。返回值是一个 4 整数元组:(左、上、宽、高)。可以将此元组传递给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()函数可能是您最想使用的函数:
>>> import pyscreeze >>> x, y = pyscreeze.locateCenterOnScreen('calc7key.png') >>> pyscreeze.click(x, y)
在 1920 x 1080 屏幕上,定位函数调用大约需要 1 或 2 秒。这对于动作视频游戏来说可能太慢了,但适用于大多数用途和应用程序。
如果速度很重要,请安装可选的 opencv 库 ( pip install cv2 )。如果可用,locateAll计算将使用它,并且在全屏搜索中找到所有匹配项的时间不到 1 毫秒。(这不包括捕获屏幕截图所需的时间。)
有几个“定位”功能。他们都开始看屏幕(或图像)的左上角,然后看向左然后向下看。参数可以是
locateOnScreen(image, grayscale=False) - 返回屏幕上第一个找到的图像实例的(左、上、宽、高)坐标。如果在屏幕上找不到,则返回 None。
locateCenterOnScreen(image, grayscale=False) - 返回屏幕上第一个找到的图像实例的中心的 (x, y) 坐标。如果在屏幕上找不到,则返回 None。
locateAllOnScreen(image, grayscale=False) - 返回一个生成器,该生成器为在屏幕上找到图像的位置生成 (left, top, width, height) 元组。
locate(needleImage, haystackImage, grayscale=False) - 返回haystackImage中第一个找到的needleImage实例的(左、上、宽、高)坐标。如果在屏幕上找不到,则返回 None。
locateAll(needleImage, haystackImage, grayscale=False) - 返回一个生成器,该生成器为在haystackImage中找到needleImage的位置生成 (left, top, width, height) 元组。
“全部定位”函数可用于 for 循环或传递给list():
>>> import pyscreeze >>> for pos in pyscreeze.locateAllOnScreen('someButton.png') ... print(pos) ... (1101, 252, 50, 50) (59, 481, 50, 50) (1395, 640, 50, 50) (1838, 676, 50, 50) >>> list(pyscreeze.locateAllOnScreen('someButton.png')) [(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]
灰度匹配
或者,您可以将grayscale=True传递给定位函数以稍微加快速度(大约 30%-ish)。这会降低图像和屏幕截图中的颜色饱和度,加快定位速度,但可能会导致误报匹配。
>>> import pyscreeze >>> button7location = pyscreeze.locateOnScreen('calc7key.png', grayscale=True) >>> button7location (1416, 562, 50, 41)
像素匹配
要获取屏幕截图中像素的 RGB 颜色,请使用 Image 对象的getpixel()方法:
>>> import pyscreeze >>> im = pyscreeze.screenshot() >>> im.getpixel((100, 200)) (130, 135, 144)
或者作为单个函数,调用pixel() PyScreez 函数,它是前面调用的包装器:
>>> import pyscreeze >>> pyscreeze.pixel(100, 200) (130, 135, 144)
如果您只需要验证单个像素是否与给定像素匹配,请调用pixelMatchesColor()函数,将其表示的颜色的 X 坐标、Y 坐标和 RGB 元组传递给它:
>>> import pyscreeze >>> pyscreeze.pixelMatchesColor(100, 200, (130, 135, 144)) True >>> pyscreeze.pixelMatchesColor(100, 200, (0, 0, 0)) False
可选的公差关键字参数指定红色、绿色和蓝色值在仍然匹配的情况下可以变化多少:
>>> import pyscreeze >>> pyscreeze.pixelMatchesColor(100, 200, (130, 135, 144)) True >>> pyscreeze.pixelMatchesColor(100, 200, (140, 125, 134)) False >>> pyscreeze.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10) True
支持
如果您发现此项目有帮助并愿意支持其开发,[考虑在 Patreon 上向其创建者捐款]( https://www.patreon.com/AlSweigart )。