Fastest way to take a screenshot with python on windows

Viewed 49979

What's the fastest way to take a screenshot on windows? PIL.ImageGrab is rather slow.. it takes between 4-5 seconds to take 30 screenshots of the same small window. Taking screenshots of the whole desktop is even slower.

4 Answers

You can try my newly created project DXcam: I think for raw speed it's the fastest out there (in python, and without going too deep into the rabbit hole). It's originally created for a deep learning pipeline for FPS games where the higher FPS you get the better. Plus I (am trying to) design it to be user-friendly: For a screenshot just do

import dxcam
camera = dxcam.create()
frame = camera.grab()  # full screen
frame = camera.grab(region=(left, top, right, bottom))  # region

For screen capturing:

camera.start(target_fps=60)  # threaded
for i in range(1000):
    image = camera.get_latest_frame()  # Will block until new frame available
camera.stop()

I copied the part of the benchmarks section from the readme:

DXcam python-mss D3DShot
Average FPS 238.79 75.87 118.36
Std Dev 1.25 0.5447 0.3224

The benchmarks is conducted through 5 trials on my 240hz monitor with a constant 240hz rendering rate synced w/the monitor (using blurbuster ufo test).

You can read more about the details here: https://github.com/ra1nty/DXcam

You can use package mss:

Save screenshot to image file

import mss

with mss.mss() as sct:
    filename = sct.shot(output="output.png")

Get the numpy representation of screenshot

import mss
import numpy as np

with mss.mss() as sct:
    monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
    img_array = np.array(sct.grab(monitor))
    # Do whatever you want...
Related