使用区域加速 pyautogui screenshot()

Speeding up pyautogui screenshot() with region

我正在尝试加快 pyautogui 中的屏幕截图功能,因为我只需要屏幕的一小部分。区域变量应该是通向这个 a.k.a 的途径。 pyautogui.screenshot(region=(0,0,400,300))。然而,在做了一些测试后,我发现无论区域的大小,截取屏幕截图所花费的时间总是相同的 (~250ms)。

此外,将屏幕截图保存到文件时 pyautogui.screenshot('dummy.png', region=(0,0,400,300)) 区域变量似乎无关紧要,并且无论如何都会保存整个屏幕。关于为什么这不能正常工作的任何想法?

运行 这个在 OS X

在 macOS 上,PyAutoGUI 仅调用 screencapture 实用程序。所以它很慢。 你可以试试 MSS, it will be blazing fast and requires not other tools/modules. This is an example you could try (copied from the documentation):

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')

这是我为帮助人们创建区域而制作的脚本!

首先将光标放在您要捕获的区域的左上角,然后按回车键。

下一步移动到您要查找的区域的右下角,然后再次按回车键。

它会打印出你的区域co-ordinates,继续使用它和下面的代码

pyautogui.locateOnScreen('example.png', region=(0,0,0,0)) 

#Fill in your region co-ords there

希望对您有所帮助!