4

I've been struggling to come up with a script that allows me to take screenshots of my desktop more than once per every second. I'm using Win10.

PIL:

from PIL import ImageGrab
import time

while True:
    im = ImageGrab.grab()
    fname = "dropfolder/%s.png" %int(time.time())
    im.save(fname,'PNG') 

Results 1.01 seconds per image.

PyScreeze (https://github.com/asweigart/pyscreeze):

import pyscreeze
import time

while True:
    fname = "dropfolder/%s.png" %int(time.time())
    x = pyscreeze.screenshot(fname)

Results 1.00 seconds per image.

Win32:

import win32gui
import win32ui 
import win32con
import time

w=1920 #res
h=1080 #res

while True:
    wDC = win32gui.GetWindowDC(0)
    dcObj=win32ui.CreateDCFromHandle(wDC)
    cDC=dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
    cDC.SelectObject(dataBitMap)
    cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
    fname = "dropfolder/%s.png" %int(time.time())
    dataBitMap.SaveBitmapFile(cDC, fname)
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(0, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())

Results 1.01 seconds per image.

Then I stumbled into thread (Fastest way to take a screenshot with python on windows) where it was suggested that gtk would yield phenomenal results.

However using gtk:

import gtk
import time

img_width = gtk.gdk.screen_width()
img_height = gtk.gdk.screen_height()

while True:
    screengrab = gtk.gdk.Pixbuf(
        gtk.gdk.COLORSPACE_RGB,
        False,
        8,
        img_width,
        img_height
    )

    fname = "dropfolder/%s.png" %int(time.time())
    screengrab.get_from_drawable(
        gtk.gdk.get_default_root_window(),
        gtk.gdk.colormap_get_system(),
        0, 0, 0, 0,
        img_width,
        img_height
        ).save(fname, 'png')

Results 2.34 seconds per image.

It seems to me like I'm doing something wrong, because people have been getting great results with gtk.

Any advices how to speed up the process?

Thanks!

Community
  • 1
  • 1
jjjayn
  • 555
  • 5
  • 10
  • 19

3 Answers3

5

Your first solution should be giving you more than one picture per second. The problem though is that you will be overwriting any pictures that occur within the same second, i.e. they will all have the same filename. To get around this you could create filenames that include 10ths of a second as follows:

from PIL import ImageGrab
from datetime import datetime

while True:
    im = ImageGrab.grab()
    dt = datetime.now()
    fname = "pic_{}.{}.png".format(dt.strftime("%H%M_%S"), dt.microsecond // 100000)
    im.save(fname, 'png') 

On my machine, this gave the following output:

pic_1143_24.5.png
pic_1143_24.9.png
pic_1143_25.3.png
pic_1143_25.7.png
pic_1143_26.0.png
pic_1143_26.4.png
pic_1143_26.8.png
pic_1143_27.2.png
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
3

In case anyone cares in 2022: 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

ra1n
  • 61
  • 1
2

This solution uses d3dshot.

def d3dgrab(rect=(0, 0, 0, 0), spath=r".\\pictures\\cache\\", sname="", title=""):
    """ take a screenshot by rect. """

    sname = sname if sname else time.strftime("%Y%m%d%H%M%S000.jpg", time.localtime())
    while os.path.isfile("%s%s" % (spath, sname)):
        sname = "%s%03d%s" % (sname[:-7], int(sname[-7:-4]) + 1, sname[-4:])

    xlen = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
    ylen = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)

    assert 0 <= rect[0] <= xlen and 0 <= rect[2] <= xlen, ValueError("Illegal value of X coordination in rect: %s" % rect)
    assert 0 <= rect[1] <= ylen and 0 <= rect[3] <= ylen, ValueError("Illegal value of Y coordinatoin in rect: %s" % rect)

    if title:
        hdl = win32gui.FindWindow(None, title)
        if hdl != win32gui.GetForegroundWindow():
            win32gui.SetForegroundWindow(hdl)
        rect = win32gui.GetWindowRect(hdl)
    elif not sum(rect):
        rect = (0, 0, xlen, ylen)
    
    d = d3dshot.create(capture_output="numpy")
    return d.screenshot_to_disk(directory=spath, file_name=sname, region=rect)

I think it can be helped

sname = sname if sname else time.strftime("%Y%m%d%H%M%S000.jpg", time.localtime())
while os.path.isfile("%s%s" % (spath, sname)):
    sname = "%s%03d%s" % (sname[:-7], int(sname[-7:-4]) + 1, sname[-4:])

And it's fastest way to take screenshot I found.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
davee
  • 21
  • 2