python截图pyscreenshot

tldr: Use Pillow

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, 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:

home: https://github.com/ponty/pyscreenshot

PYPI: https://pypi.python.org/pypi/pyscreenshot

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.6, 3.7, 3.8, 3.9

It has wrappers for various back-ends:

scrot

maim

ImageMagick

Pillow

PyQt4

PyQt5

PySide

PySide2

wxPython

Quartz (Mac)

screencapture (Mac)

gnome-screenshot

Python MSS

Grim

Old removed backends: QtPy, PyGTK

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:

KDE Wayland has on screen notification

gnome-screenshot has Flash effect (https://bugzilla.gnome.org/show_bug.cgi?id=672759)

Installation:

$python3 -m pip install Pillow pyscreenshot

Examples

# pyscreenshot/examples/grabfullscreen.py"Grab the whole screen"importpyscreenshotasImageGrab# grab fullscreenim=ImageGrab.grab()# save image fileim.save("fullscreen.png")

# pyscreenshot/examples/grabbox.py"Grab the part of the screen"importpyscreenshotasImageGrab# part of the screenim=ImageGrab.grab(bbox=(10,10,510,510))# X1,Y1,X2,Y2# save image fileim.save("box.png")

# pyscreenshot/examples/virtdisp.py"Create screenshot of xmessage with Xvfb"fromtimeimportsleepfromeasyprocessimportEasyProcessfrompyvirtualdisplayimportDisplayimportpyscreenshotasImageGrabwithDisplay(size=(100,60))asdisp:# start Xvfb display# display is availablewithEasyProcess(["xmessage","hello"]):# start xmessagesleep(1)# wait for diplaying windowimg=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 20.04 X11

Versions:

$python3 -m pyscreenshot.check.versionspython              3.8.5pyscreenshot        2.3pil                  8.0.1mss                  6.1.0scrot                1.2grim                ?.?maim                5.5.3imagemagick          6.9.10pyqt5                5.14.1pyqt                pyside2              5.14.0pyside              wx                  4.0.7pygdk3              3.36.0mac_screencapture    mac_quartz          gnome_dbus          ?.?gnome-screenshot    3.36.0kwin_dbus            ?.?

$python3 -m pyscreenshot.check.speedtestn=10------------------------------------------------------default            1    sec (  101 ms per call)pil                1.7  sec (  166 ms per call)mss                1.9  sec (  191 ms per call)scrot              0.97 sec (  97 ms per call)grim                maim                1.4  sec (  144 ms per call)imagemagick        2.4  sec (  235 ms per call)pyqt5              4.3  sec (  429 ms per call)pyqt                pyside2            4.2  sec (  423 ms per call)pyside              wx                  4.1  sec (  412 ms per call)pygdk3              2    sec (  204 ms per call)mac_screencapture  mac_quartz          gnome_dbus          1.4  sec (  144 ms per call)gnome-screenshot    3.8  sec (  381 ms per call)kwin_dbus         

$python3 -m pyscreenshot.check.speedtest --childprocess 0n=10------------------------------------------------------default            0.11 sec (  10 ms per call)pil                0.09 sec (    8 ms per call)mss                0.15 sec (  15 ms per call)scrot              0.95 sec (  95 ms per call)grim                maim                1.5  sec (  145 ms per call)imagemagick        2.4  sec (  235 ms per call)pyqt5              1.1  sec (  114 ms per call)pyqt                pyside2            1.2  sec (  118 ms per call)pyside              wx                  0.43 sec (  43 ms per call)pygdk3              0.16 sec (  15 ms per call)mac_screencapture  mac_quartz          gnome_dbus          1.5  sec (  147 ms per call)gnome-screenshot    3.8  sec (  383 ms per call)kwin_dbus         

You can force a backend:

importpyscreenshotasImageGrabim=ImageGrab.grab(backend="scrot")

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

# best performanceimportpyscreenshotasImageGrabim=ImageGrab.grab(backend="mss",childprocess=False)

Wayland

Wayland is supported with two setups:

using D-Bus on GNOME or KDE. Python 3 only.

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:

use X if DISPLAY variable exists and XDG_SESSION_TYPE variable != "wayland"

use Wayland if 1. is not successful

Dependencies

Only pure python modules are used:

EasyProcess for calling programs

entrypoint2 for generating command line interface

MSS backend is added because it is very fast and pure and multiplatform

jeepney for D-Bus calls

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,384评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,845评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,148评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,640评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,731评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,712评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,703评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,473评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,915评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,227评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,384评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,063评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,706评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,302评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,531评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,321评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,248评论 2 352

推荐阅读更多精彩内容