• Stars
    star
    495
  • Rank 88,974 (Top 2 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created almost 14 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.

TL;DR: Use Pillow. If Pillow doesn't work or it's slow then try pyscreenshot.

The pyscreenshot module is obsolete in most cases. It was created because PIL ImageGrab module worked on Windows only, but now Linux and macOS are also supported by Pillow. There are some features in pyscreenshot which can be useful in special cases: flexible backends with the same interface, Wayland support, sometimes better performance, optional subprocessing.

The module can be used to copy the contents of the screen to a Pillow image memory using various back-ends. Replacement for the ImageGrab Module.

For handling image memory (e.g. saving to file, converting,..) please read Pillow documentation.

Links:

workflow

Goal: Pyscreenshot tries to allow to take screenshots without installing 3rd party libraries. It is cross-platform. It is only a pure Python wrapper, a thin layer over existing back-ends. Its strategy should work on most Linux distributions: a lot of back-ends are wrapped, if at least one exists then it works, if not then one back-end should be installed.

Features:

  • Cross-platform wrapper
  • Capturing the whole desktop or an area
  • saving to Pillow image memory
  • some back-ends are based on this discussion: http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux
  • pure Python library
  • supported Python versions: 3.9, 3.10, 3.11
  • It has wrappers for various back-ends:
  • Old removed backends:
    • PyGTK
    • QtPy,
    • PySide
    • PyQt4
    • KDE Plasma (D-Bus: org.kde.kwin.Screenshot)
  • Performance is not the main target for this library, but you can benchmark the possible settings and choose the fastest one.
  • Interactivity is not supported.
  • Mouse pointer is not visible.

Known problems:

Comparison:

system pyscreenshot 3.1 pillow 9.4.0 mss 7.0.1
Ubuntu 22.04
Kubuntu 22.04
Xubuntu 22.04
Lubuntu 22.04
Ubuntu Server 22.04+sway+grim

Installation:

$ python3 -m pip install Pillow pyscreenshot

Examples

# pyscreenshot/examples/grabfullscreen.py

"Grab the whole screen"
import pyscreenshot as ImageGrab

# grab fullscreen
im = ImageGrab.grab()

# save image file
im.save("fullscreen.png")
# pyscreenshot/examples/grabbox.py

"Grab the part of the screen"
import pyscreenshot as ImageGrab

# part of the screen
im = ImageGrab.grab(bbox=(10, 10, 510, 510))  # X1,Y1,X2,Y2

# save image file
im.save("box.png")
# pyscreenshot/examples/virtdisp.py

"Create screenshot of xmessage with Xvfb"
from time import sleep

from easyprocess import EasyProcess
from pyvirtualdisplay import Display

import pyscreenshot as ImageGrab

with Display(size=(100, 60)) as disp:  # start Xvfb display
    # display is available
    with EasyProcess(["xmessage", "hello"]):  # start xmessage
        sleep(1)  # wait for displaying window
        img = ImageGrab.grab()
img.save("xmessage.png")

Image:

Performance

The performance can be checked with pyscreenshot.check.speedtest module. Backends are started in a subprocess with default (safest) settings which is necessary to isolate them from the main process and from each other. Disabling this option makes performance much better, but it may cause problems in some cases.

Test on Ubuntu 22.04 X11

Versions:

$ python3 -m pyscreenshot.check.versions
python               3.10.6
pyscreenshot         3.1
pil                  9.0.1
mss                  7.0.1
scrot                1.7
grim                 ?.?
maim                 5.6.3
imagemagick          6.9.11
pyqt5                5.15.6
pyside2              5.15.2
wx                   4.0.7
pygdk3               3.42.1
mac_screencapture    
mac_quartz           
freedesktop_dbus     ?.?
gnome_dbus           ?.?
gnome-screenshot     41.0

Backends are started in a subprocess (safest):

$ python3 -m pyscreenshot.check.speedtest

n=10
------------------------------------------------------
default             	1    sec	(  100 ms per call)
pil                 	1.7  sec	(  167 ms per call)
mss                 	2    sec	(  197 ms per call)
scrot               	1    sec	(  100 ms per call)
grim                	
maim                	1.4  sec	(  135 ms per call)
imagemagick         	2.2  sec	(  221 ms per call)
pyqt5               	4    sec	(  403 ms per call)
pyside2             	3.9  sec	(  394 ms per call)
wx                  	2.9  sec	(  293 ms per call)
pygdk3              	2.2  sec	(  218 ms per call)
mac_screencapture   	
mac_quartz          	
gnome_dbus          	
gnome-screenshot    	4    sec	(  401 ms per call)

Backends are started without subprocess (fastest):

$ python3 -m pyscreenshot.check.speedtest --childprocess 0

n=10
------------------------------------------------------
default             	0.13 sec	(   12 ms per call)
pil                 	0.12 sec	(   11 ms per call)
mss                 	0.2  sec	(   19 ms per call)
scrot               	1    sec	(   99 ms per call)
grim                	
maim                	1.3  sec	(  134 ms per call)
imagemagick         	2.2  sec	(  218 ms per call)
pyqt5               	1    sec	(  104 ms per call)
pyside2             	1    sec	(  101 ms per call)
wx                  	0.34 sec	(   33 ms per call)
pygdk3              	0.23 sec	(   23 ms per call)
mac_screencapture   	
mac_quartz          	
gnome_dbus          	
gnome-screenshot    	4.4  sec	(  437 ms per call)

You can force a backend:

import pyscreenshot as ImageGrab
im = ImageGrab.grab(backend="scrot")

You can force if subprocess is applied, setting it to False together with mss or pil gives the best performance in most cases:

# best performance
import pyscreenshot as ImageGrab
im = ImageGrab.grab(backend="mss", childprocess=False)

Wayland

Wayland is supported with these setups:

  1. using D-Bus (org.freedesktop.portal.Screenshot) on any desktop with xdg-desktop-portal.
  2. using D-Bus (org.gnome.Shell.Screenshot) on GNOME.
  3. using Grim on any Wayland compositor with wlr-screencopy-unstable-v1 support. (GNOME:no, KDE:no, Sway:yes)

If both Wayland and X are available then Wayland is preferred because Xwayland can not be used for screenshot. Rules for decision:

  1. use X if DISPLAY variable exists and XDG_SESSION_TYPE variable != "wayland"
  2. use Wayland if 1. is not successful

Dependencies

Only pure python modules are used:

  1. EasyProcess for calling programs
  2. entrypoint2 for generating command line interface
  3. MSS backend is added because it is very fast and pure and multiplatform
  4. jeepney for D-Bus calls

Test

Some Linux distributions can be tested with VirtualBox and Vagrant:

$ ./tests/vagrant/vagrant_boxes.py

Hierarchy

Alt text

More Repositories

1

PyVirtualDisplay

Python wrapper for Xvfb, Xephyr and Xvnc
Python
687
star
2

framebuffer-vncserver

VNC server for Linux framebuffer devices
C
162
star
3

EasyProcess

Easy to use python subprocess interface
Python
112
star
4

pyunpack

unpack archive files in python
Python
97
star
5

pysimavr

python wrapper for simavr which is AVR and arduino simulator
C
48
star
6

psidialogs

Python Simple Dialogs
Python
34
star
7

arduino-rtttl-player

Arduino library to play RTTTL melodies
Python
16
star
8

pysimavrgui

Simple GUI elements for AVR and arduino simulation. Programmed in python, based on pygame. Simavr is used for simulation.
Python
15
star
9

pyavrutils

pyavrutils is a Python library that can build AVR and arduino code at runtime.
Python
11
star
10

confduino

arduino library installer
Python
9
star
11

eagexp

export Eagle 6.6.0 schematic or board to image
POV-Ray SDL
9
star
12

entrypoint2

easy to use command-line interface for python modules
Python
8
star
13

discogui

Experimental Python library for discovering GUI elements.
Python
6
star
14

softusbduino

Arduino control over USB in Python
C
5
star
15

sphinxcontrib-programscreenshot

Sphinx extension to include program screenshot
Python
4
star
16

sphinxcontrib-gtkwave

Sphinx extension to include VCD (value change dump) files using GTKWave
Python
4
star
17

sphinxcontrib-eagle

Sphinx extension to include image or partlist of eagle schematic or board
Python
3
star
18

abandi

Console-based abandonware game installer
Python
3
star
19

MyElectronicProjects

Hobby electronic projects built by me
Python
3
star
20

fb-test-app

copy of https://github.com/prpplague/fb-test-app
C
2
star
21

mangui

GUI generator for command-line programs on Linux.
Python
2
star
22

serial_port_tester

Python serial port tester / monitor GUI
Python
2
star
23

nanpy-drivers

drivers for Nanpy + Arduino
C
1
star
24

StripboardProto

Stripboard based modular hardware prototyping system.
Python
1
star
25

nanpy-gui

TraitsUI based GUI for Nanpy
Python
1
star
26

electronic-measurements

Electronic measurements using Arduino, some additional hardware, USB port and Python libraries
Python
1
star