Python 螢幕截圖存檔 pyscreenshot 用法

本篇 ShengYu 要介紹如何使用 Python pyscreenshot 螢幕截圖存檔,pyscreenshot 是 PIL ImageGrab 模組在 Linux 下的替代方案,PIL 的 ImageGrab 只支援 Windows 與 macOS,所以在 Windows 與 macOS 系統下直接使用 PIL ImageGrab 模組就可以了,同樣的程式碼如果要在 Linux 下使用的話,只需要裝 pyscreenshot 這個模組即可馬上使用。

安裝 pyscreenshot 模組

1
$ pip3 install Pillow pyscreenshot

螢幕截圖輸出成 jpeg

python3-pyscreenshot-jpeg.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab()
img.save('fullscreen.jpeg', quality=70)

螢幕截圖輸出成 jpeg 並加上當下系統時間

python3-pyscreenshot-jpeg-with-time.py
1
2
3
4
5
6
7
8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab
import time

img = ImageGrab.grab()
filename = 'fullscreen-' + time.strftime('%Y-%m-%d_%H-%M-%S') + '.jpeg'
img.save(filename, quality=70)

生成的檔案名稱就會是 fullscreen-2021-06-14_21-22-40.jpeg 這樣的格式。

螢幕截圖輸出成 png

python3-pyscreenshot-png.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab()
img.save('fullscreen.png', quality=80)

螢幕截圖輸出成 bmp

python3-pyscreenshot-bmp.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab()
img.save('fullscreen.bmp')

螢幕截圖輸出成 BytesIO

以下示範 jpeg 影像格式,也可用 png 或其它,

python3-pyscreenshot-bytesio.py
1
2
3
4
5
6
7
8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab
from io import BytesIO

img_buffer = BytesIO()
img = ImageGrab.grab()
img.save(img_buffer, 'JPEG', quality=80)

螢幕截圖並顯示

python3-pyscreenshot-show.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab()
img.show()

指定範圍的螢幕截圖

以下示範指定範圍 (10,10)左上 - (510,510) 右下的 500x500 螢幕截圖,

python3-pyscreenshot-grabbox.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab(bbox=(10, 10, 510, 510)) # X1, Y1, X2, Y2
img.show()

強制指定 backend

grab() 預設會去嘗試每一種 backend,grab() 傳入指定 backend,這邊以 mss 為例,,如果很確定要使用哪種 backend 的話可以直接指定,避免無效的嘗試。

python3-pyscreenshot-mss-backend.py
1
2
3
4
5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab(backend='mss')

查詢 backend

使用 backends() 可以印出目前支援的 backends 後端,但不代表該後端就可以使用,例如 scrot 後端就要先安裝 scrot 這個指令才可以使用。

1
2
3
4
5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

print(ImageGrab.backends())

以我的 Ubuntu 16.04 為例,輸出的結果如下,

1
2
backends ['pyside2', 'mac_screencapture', 'wx', 'pyqt', 'maim', 'grim', 'gnome_dbus', 'kwin_dbus', 'gnome-screenshot', 'ima
gemagick', 'pyside', 'pil', 'scrot', 'mss', 'pyqt5', 'pygdk3', 'mac_quartz']

高效能截圖

設定 childprocess 為 False 可以獲得更好的效能,

1
2
3
4
5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyscreenshot as ImageGrab

img = ImageGrab.grab(backend="mss", childprocess=False)

使用 pyscreenshot.check.versions 模組可以看各後端的版本號碼,以我的 Ubuntu 16.04 為例,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ python3 -m pyscreenshot.check.versions
python 3.5.2
pyscreenshot 3.0
scrot
gnome-screenshot 3.18.0
mac_screencapture
pygdk3 3.20.0
pyqt5 5.5.1
pil 4.2.0
maim
wx
pyside2
pyqt
grim
mss 6.1.0
gnome_dbus ?.?
imagemagick 6.8.9
pyside
kwin_dbus ?.?
mac_quartz

使用 pyscreenshot.check.speedtest 模組可以測試各後端的效能,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ python3 -m pyscreenshot.check.speedtest

n=10
------------------------------------------------------
default 3.9 sec ( 385 ms per call)
mac_quartz
pyqt
imagemagick 2.4 sec ( 239 ms per call)
mac_screencapture
maim
pyside
grim
gnome-screenshot 2.3 sec ( 228 ms per call)
kwin_dbus
gnome_dbus
scrot
mss 2.5 sec ( 246 ms per call)
pygdk3 2.6 sec ( 263 ms per call)
wx
pyside2
pyqt5 4.8 sec ( 476 ms per call)
pil

使用 pyscreenshot.check.speedtest 模組並指定 –childprocess 0 測試各後端的效能,已我的系統來說 mss 效能最好,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ python3 -m pyscreenshot.check.speedtest --childprocess 0

n=10
------------------------------------------------------
default 0.15 sec ( 15 ms per call)
pyside2
pyqt
pyside
gnome-screenshot 2.3 sec ( 228 ms per call)
kwin_dbus
scrot
wx
pil
pygdk3 0.17 sec ( 17 ms per call)
gnome_dbus
imagemagick 2.5 sec ( 245 ms per call)
mac_screencapture
mac_quartz
maim
pyqt5 1.1 sec ( 114 ms per call)
mss 0.15 sec ( 15 ms per call)
grim

其它參考
GitHub - ponty/pyscreenshot: Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
https://github.com/ponty/pyscreenshot/
pyscreenshot · PyPI
https://pypi.org/project/pyscreenshot/