pyscreenshot

About

The pyscreenshot module can be used to copy the contents of the screen to a PIL or Pillow image memory using various back-ends. Replacement for the ImageGrab Module, which works on Windows only, so Windows users don’t need this library. For handling image memory (e.g. saving to file, converting,..) please read PIL or Pillow documentation.

Links:

Travis Coveralls Latest Version Supported Python versions License Documentation

Goal:
Pyscreenshot tries to allow to take screenshots without installing 3rd party libraries. It is cross-platform but useful for Linux based distributions. 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:
Known problems:
Similar projects:

Examples

grab and show the whole screen:

#-- include('examples/showgrabfullscreen.py') --#
import pyscreenshot as ImageGrab

if __name__ == '__main__':

    # grab fullscreen
    im = ImageGrab.grab()

    # save image file
    im.save('screenshot.png')

    # show image in a window
    im.show()
#-#

to start the example:

python3 -m pyscreenshot.examples.showgrabfullscreen

grab and show the part of the screen:

#-- include('examples/showgrabbox.py')--#
import pyscreenshot as ImageGrab

if __name__ == '__main__':
    # part of the screen
    im = ImageGrab.grab(bbox=(10, 10, 510, 510))  # X1,Y1,X2,Y2
    im.show()
#-#

to start the example:

python3 -m pyscreenshot.examples.showgrabbox

Installation

  • install Pillow (Ubuntu: sudo apt-get install python3-pil)

  • install at least one back-end

  • install the program:

    pip3 install pyscreenshot
    

Command line interface

Back-end performance:

The performance can be checked with pyscreenshot.check.speedtest.

Example:

#-- sh('python3 -m pyscreenshot.check.speedtest --virtual-display 2>/dev/null') --#

n=10
------------------------------------------------------
scrot                 6.1  sec        (  608 ms per call)
imagemagick           9.7  sec        (  969 ms per call)
wx                    4.1  sec        (  408 ms per call)
pygdk3                3.3  sec        (  328 ms per call)
qtpy                  6.9  sec        (  687 ms per call)
pyqt5                 6.9  sec        (  687 ms per call)
pyqt                  6.4  sec        (  644 ms per call)
pyside2               6.7  sec        (  671 ms per call)
pyside                6.5  sec        (  652 ms per call)
gnome-screenshot      12   sec        ( 1209 ms per call)
#-#

Print versions:

#-- sh('python3 -m pyscreenshot.check.versions 2> /dev/null ')--#
python               3.7.3
pyscreenshot         0.7
scrot                1.1.1
imagemagick          6.9.10
wx                   4.0.4
pygdk3               3.32.0
qtpy                 1.3.1
pyqt5                5.12.1
pyqt                 4.12.1
pyside2              5.11.2
pyside               1.2.2
pygtk                missing
gnome-screenshot     3.30.0
#-#

Wayland

On Wayland only the gnome-screenshot back-end works:

im = ImageGrab.grab(backend='gnome-screenshot')

Hierarchy

digraph G {
    rankdir=LR;
    node [fontsize=8];
    fontsize=8;

    subgraph cluster_0 {
        label = "pyscreenshot";
        style=filled;
        fillcolor=lightgrey;
        subgraph cluster_1 {
            label = "API";
            style=filled;
            fillcolor=white;

            pyscreenshot;
        }
        subgraph cluster_2 {
            style=filled;
            fillcolor=white;
            label = "plugins";

            pyscreenshot -> GtkPixbufWrapper;
            pyscreenshot -> Qt4GrabWindowWrapper;
            pyscreenshot -> Qt5GrabWindowWrapper;
            pyscreenshot -> PySideGrabWindowWrapper;
            pyscreenshot -> PySide2GrabWindowWrapper;
            pyscreenshot -> QtPyGrabWindowWrapper;
            pyscreenshot -> PilWrapper;
            pyscreenshot -> ImagemagickWrapper;
            pyscreenshot -> WxScreenWrapper;
            pyscreenshot -> ScrotWrapper;
            pyscreenshot -> MaimWrapper;
            pyscreenshot -> MacQuartzWrapper
            pyscreenshot -> ScreencaptureWrapper
            pyscreenshot -> GnomeScreenshotWrapper
            pyscreenshot -> Gdk3PixbufWrapper
        }
    }
    subgraph cluster_3 {
        PIL;
        wxPython;
        PyQt4;
        PyQt5;
        PySide;
        PySide2;
        QtPy;
        PyGTK;
        screencapture;
        Quartz;
        Scrot;
        Maim;
        Imagemagick;
        "gnome-screenshot";
    }
    subgraph cluster_4 {
        label = "GUI library";
        Qt4;
        Qt5;
        wxWidgets;
        "GTK+";
    }

    Qt4GrabWindowWrapper -> PyQt4 -> Qt4;
    PyQt4 -> Qt5;
    Qt5GrabWindowWrapper -> PyQt5 -> Qt5;
    PySideGrabWindowWrapper -> PySide -> Qt4;
    PySide2GrabWindowWrapper -> PySide2 -> Qt5;

    QtPyGrabWindowWrapper -> QtPy;
    QtPy -> PySide;
    QtPy -> PySide2;
    QtPy -> PyQt4;
    QtPy -> PyQt5;

    Qt4 -> MacOS;
    Qt4 -> Windows;
    Qt4 -> X11;
    Qt5 -> MacOS;
    Qt5 -> Windows;
    Qt5 -> X11;

    PilWrapper -> PIL -> Windows;
    ImagemagickWrapper -> Imagemagick -> X11;
    ScrotWrapper -> Scrot -> X11;
    MaimWrapper -> Maim -> X11;

    GnomeScreenshotWrapper -> "gnome-screenshot" -> X11;
    "gnome-screenshot" -> Wayland;

    GtkPixbufWrapper -> PyGTK -> "GTK+";
    //"GTK+" -> MacOS;
    "GTK+" -> Windows;
    "GTK+" -> X11;

    Gdk3PixbufWrapper -> PyGObject -> GdkPixbuf;
    GdkPixbuf -> MacOS;
    GdkPixbuf -> Windows;
    GdkPixbuf -> X11;

    WxScreenWrapper -> wxPython -> wxWidgets;
    wxWidgets -> "GTK+";
    wxWidgets -> MacOS;
    wxWidgets -> Windows;
    wxWidgets -> X11;

    MacQuartzWrapper -> Quartz -> MacOS;
    ScreencaptureWrapper -> screencapture -> MacOS;

    application -> pyscreenshot;

}

API

pyscreenshot.backend_version(backend)[source]

Back-end version.

Parameters:backend – back-end (examples:scrot, wx,..)
Returns:version as string
pyscreenshot.backends()[source]

Back-end names as a list.

Returns:back-ends as string list
pyscreenshot.grab(bbox=None, childprocess=None, backend=None)[source]

Copy the contents of the screen to PIL image memory.

Parameters:
  • bbox – optional bounding box (x1,y1,x2,y2)
  • childprocess – run back-end in new process using popen. (bool) This can isolate back-ends from each other and from main process. It can be forced if set, otherwise childprocess is automatic.
  • backend – back-end can be forced if set (examples:scrot, wx,..), otherwise back-end is automatic