我正在尝试加快 pyautogui 中的屏幕截图功能,因为我只需要屏幕的一小部分。据推测,区域变量就是实现此目的的方法。
pyautogui.screenshot(region=(0,0,400,300))
。然而,经过一些测试后,我发现无论区域大小如何,截取屏幕截图所需的时间都是相同的(约 250 毫秒)。
此外,将屏幕截图保存到文件时
pyautogui.screenshot('dummy.png', region=(0,0,400,300))
区域变量似乎并不重要,并且无论如何都会保存整个屏幕。关于为什么这不能正常工作的任何想法?
在 OS X 上运行此程序
在 macOS 上,PyAutoGUI 调用 screencapture 实用程序。所以它很慢。 您可以尝试一下MSS,它的速度会非常快,并且不需要其他工具/模块。这是您可以尝试的示例(从文档复制):
import mss
import mss.tools
with mss.mss() as sct:
# The screen part to capture
region = {'top': 0, 'left': 0, 'width': 400, 'height': 300}
# Grab the data
img = sct.grab(region)
# Save to the picture file
mss.tools.to_png(img.rgb, img.size, output='dummy.png')
import pyautogui
get1 = input('\nPlace cursor at the top left of the region you want to capture, and then press enter \n')
pos1 = pyautogui.position()
get2 = input('Now place your cursor at the bottom right of the region you want to capture, and press enter \n')
pos2 = pyautogui.position()
width = pos2[0] - pos1[0]
height = pos2[1] - pos1[1]
print('Your region is... \n')
print('region=('+str(pos1[0])+','+str(pos1[1])+','+str(width)+','+str(height)+') \n')
这是我为帮助人们创建区域而编写的脚本!
首先将光标放在要捕获的区域的左上角,然后按 Enter。
接下来移动到您要查找的区域的右下角,然后再次按 Enter。
它将打印出您的区域坐标,继续并将其与以下代码一起使用
pyautogui.locateOnScreen('example.png', region=(0,0,0,0))
#Fill in your region co-ords there
希望这有帮助!