Search code examples
pythonscreenshotpyautogui

Speeding up pyautogui screenshot() with region


I'm trying to speed up the screenshot function in pyautogui since I only need a small portion of the screen. The region variable is supposedly the way to this a.k.a. pyautogui.screenshot(region=(0,0,400,300)). However after doing some testing I found that no matter the size of the region it always takes the same amount of time to take the screenshot (~250ms).

Also when saving the screenshot to a file pyautogui.screenshot('dummy.png', region=(0,0,400,300)) the region variable does not seem to matter and the whole screen is saved regardless. Any ideas on why this is not working properly?

Running this on OS X


Solution

  • On macOS, PyAutoGUI calls the screencapture utility. So it is slow. You can give it a try to MSS, it will be blazing fast and requires no 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')