Skip to content

Commit 962ff34

Browse files
authored
Add more examples
- doc: add an example about capturing part of the monitor 2 - doc: add an example about computing BGRA values to RGB
1 parent 0e3b484 commit 962ff34

File tree

8 files changed

+222
-35
lines changed

8 files changed

+222
-35
lines changed

CHANGELOG

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,75 @@ History:
33
<see Git checking messages for history>
44

55
dev 2018/xx/xx
6-
- add PNG compression level control
6+
- removed support for Python 3.4
77
- MSS: add the Screenshot.bgra attribute
88
- MSS: speed-up grabbing on the 3 platforms
9+
- tools: add PNG compression level control to to_png()
910
- tests: add leaks.py and benchmarks.py for manual testing
11+
- doc: add an example about capturing part of the monitor 2
12+
- doc: add an example about computing BGRA values to RGB
1013

1114
3.1.2 2018/01/05
1215
- removed support for Python 3.3
13-
- CI: build the documentation
14-
- doc: improvements and fixes (fix #37)
1516
- MSS: possibility to get the whole PNG raw bytes
1617
- Windows: capture all visible windows
18+
- doc: improvements and fixes (fix #37)
19+
- CI: build the documentation
1720

1821
3.1.1 2017/11/27
1922
- MSS: add the 'mss' entry point
2023

2124
3.1.0 2017/11/16
2225
- new contributor: Karan Lyons
23-
- add the 'Say Thanks' button
24-
- doc: several fixes (fix #22)
25-
- tests: a lot of tests added for better coverage
2626
- MSS: add more way of customization to the output argument of save()
2727
- MSS: possibility to use custom class to handle screen shot data
2828
- Mac: properly support all display scaling and resolutions (fix #14, #19, #21, #23)
2929
- Mac: fix memory leaks (fix #24)
3030
- Linux: handle bad display value
3131
- Windows: take into account zoom factor for high-DPI displays (fix #20)
32+
- doc: several fixes (fix #22)
33+
- tests: a lot of tests added for better coverage
34+
- add the 'Say Thanks' button
3235

3336
3.0.1 2017/07/06
3437
- fix examples links
3538

3639
3.0.0 2017/07/06
3740
- big refactor, introducing the ScreenShot class
38-
- add Numpy array interface support
39-
- examples: add OpenCV/Numpy, PIL pixels, FPS
41+
- MSS: add Numpy array interface support to the Screenshot class
42+
- docs: add OpenCV/Numpy, PIL pixels, FPS
4043

4144
2.0.22 2017/04/29
4245
- new contributors: David Becker, redodo
43-
- add an example to capture only a part of the screen
44-
- better use of exception mechanism
46+
- MSS: better use of exception mechanism
4547
- Linux: use of hasattr to prevent Exception on early exit
4648
- Mac: take into account extra black pixels added when screen with is not divisible by 16 (fix #14)
49+
- docs: add an example to capture only a part of the screen
4750

4851
2.0.18 2016/12/03
4952
- change license to MIT
5053
- new contributor: Jochen 'cycomanic' Schroeder
51-
- add type hints
52-
- add documentation (fix #10)
53-
- add tests and use Travis CI (fix #9)
54-
- remove unused code (reported by Vulture)
54+
- MSS: add type hints
55+
- MSS: remove unused code (reported by Vulture)
5556
- Linux: remove MSS library
5657
- Linux: insanely fast using only ctypes
5758
- Linux: skip unused monitors
5859
- Linux: use errcheck instead of deprecated restype with callable (fix #11)
5960
- Linux: fix security issue (reported by Bandit)
61+
- docs: add documentation (fix #10)
62+
- tests: add tests and use Travis CI (fix #9)
6063

6164
2.0.0 2016/06/04
6265
- split the module into several files
63-
- add issue and pull request templates
64-
- a lot of code refactor and optimizations
66+
- MSS: a lot of code refactor and optimizations
6567
- MSS: rename save_img() to to_png()
6668
- MSS: save(): replace 'screen' argument by 'mon'
6769
- Mac: get rid of the PyObjc module, 100% ctypes
6870
- Linux: prevent segfault when DISPLAY is set but no X server started
6971
- Linux: prevent segfault when Xrandr is not loaded
7072
- Linux: get_pixels() insanely fast, use of MSS library (C code)
7173
- Windows: fix #6, screen shot not correct on Windows 8
74+
- add issue and pull request templates
7275

7376
1.0.2 2016/04/22
7477
- MSS: fix non existent alias
@@ -78,28 +81,28 @@ dev 2018/xx/xx
7881

7982
1.0.0 2015/04/16
8083
- Python 2.6 to 3.5 ready
81-
- code purgation and review, no more debug informations
82-
- delete --debug argument
84+
- MSS: code purgation and review, no more debug informations
8385
- MSS: fix #5, add a shortcut to take automatically use the proper MSS class
8486
- MSS: few optimizations into save_img()
8587
- Darwin: remove rotation from informations returned by enum_display_monitors()
8688
- Linux: fix object has no attribute 'display' into __del__
8789
- Linux: use of XDestroyImage() instead of XFree()
8890
- Linux: optimizations of get_pixels()
8991
- Windows: huge optimization of get_pixels()
92+
- CLI: delete --debug argument
9093

9194
0.1.1 2015/04/10
92-
- little code review
93-
- add doc/TESTING
94-
- remove Bonus section from README.rst
95-
- tests: remove test-linux binary
95+
- MSS: little code review
9696
- Linux: fix monitor count
97+
- tests: remove test-linux binary
98+
- docs: add doc/TESTING
99+
- docs: remove Bonus section from README.rst
97100

98101
0.1.0 2015/04/10
99-
- fix code with YAPF tool
100-
- better tests and examples
102+
- MSS: fix code with YAPF tool
101103
- Linux: fully functional using Xrandr library
102104
- Linux: code purgation (no more XML files to parse)
105+
- docs: better tests and examples
103106

104107
0.0.8 2015/02/04
105108
- new contributors: sergey-vin, Alexander 'thehesiod' Mohr
@@ -113,16 +116,16 @@ dev 2018/xx/xx
113116

114117
0.0.6 2014/03/19
115118
- new contributor: Sam from sametmax.com
116-
- PEP8 compliant
117119
- Python 3.4 ready
118-
- review module structure to fit the "Code Like a Pythonista: Idiomatic Python"
119-
- refactoring of all enum_display_monitors() methods
120-
- fix misspellings using 'codespell' tool
121-
- possibility to append '--debug' to the command line
122-
- better way to manage output filenames (callback)
123-
- several fixes here and there, code refactoring
120+
- PEP8 compliant
121+
- MSS: review module structure to fit the "Code Like a Pythonista: Idiomatic Python"
122+
- MSS: refactoring of all enum_display_monitors() methods
123+
- MSS: fix misspellings using 'codespell' tool
124+
- MSS: better way to manage output filenames (callback)
125+
- MSS: several fixes here and there, code refactoring
124126
- MSS: moved into a MSS:save_img() method
125127
- Linux: add XFCE4 support
128+
- CLI: possibility to append '--debug' to the command line
126129

127130
0.0.5 2013/11/01
128131
- MSS: code simplified

docs/source/examples.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ You can capture only a part of the screen:
4545

4646
.. versionadded:: 3.0.0
4747

48+
49+
Part of the screen of the 2nd monitor
50+
-------------------------------------
51+
52+
This is an example of capturing some part of the screen of the monitor 2:
53+
54+
.. literalinclude:: examples/part_of_screen_monitor_2.py
55+
:lines: 9-
56+
57+
.. versionadded:: 3.0.0
58+
59+
4860
Use PIL bbox style and percent values
4961
-------------------------------------
5062

@@ -120,3 +132,37 @@ Simple naive benchmark to compare with `Reading game frames in Python with OpenC
120132
:lines: 12-
121133

122134
.. versionadded:: 3.0.0
135+
136+
137+
BGRA to RGB
138+
===========
139+
140+
Differents possiblities to convert raw BGRA values to RGB::
141+
142+
def mss_rgb(im):
143+
""" Better than Numpy versions, but slower than Pillow. """
144+
return im.rgb
145+
146+
147+
def numpy_flip(im):
148+
""" Most efficient Numpy version as of now. """
149+
frame = numpy.array(im, dtype=numpy.uint8)
150+
return numpy.flip(frame[:, :, :3], 2).tobytes()
151+
152+
153+
def numpy_slice(im):
154+
""" Slow Numpy version. """
155+
return numpy.array(im, dtype=numpy.uint8)[..., [2, 1, 0]].tobytes()
156+
157+
158+
def pil_frombytes(im):
159+
""" Efficient Pillow version. """
160+
return Image.frombytes('RGB', im.size, im.bgra, 'raw', 'BGRX').tobytes()
161+
162+
163+
with mss.mss() as sct:
164+
im = sct.grab(sct.monitors[1])
165+
rgb = pil_frombytes(im)
166+
...
167+
168+
.. versionadded:: 3.2.0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# coding: utf-8
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
5+
6+
Example to capture part of the screen of the monitor 2.
7+
"""
8+
9+
import mss
10+
import mss.tools
11+
12+
13+
with mss.mss() as sct:
14+
# Get information of monitor 2
15+
monitor_number = 2
16+
mon = sct.monitors[monitor_number]
17+
18+
# The screen part to capture
19+
monitor = {
20+
'top': mon['top'] + 100, # 100px from the top
21+
'left': mon['left'] + 100, # 100px from the left
22+
'width': 160,
23+
'height': 135,
24+
'mon': monitor_number,
25+
}
26+
output = 'sct-mon{mon}_{top}x{left}_{width}x{height}.png'.format(**monitor)
27+
28+
# Grab the data
29+
sct_img = sct.grab(monitor)
30+
31+
# Save to the picture file
32+
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
33+
print(output)

docs/source/examples/pil.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
sct_img = sct.grab(monitor)
1818

1919
# Create the Image
20-
img = Image.frombytes('RGB', sct_img.size, sct_img.rgb)
20+
img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
21+
# The same, but less efficient:
22+
# img = Image.frombytes('RGB', sct_img.size, sct_img.rgb)
2123

2224
# And save it!
2325
output = 'monitor-{0}.png'.format(num)

docs/source/support.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Abandoned
2424
=========
2525

2626
- Python 2.6 (2016-10-08)
27-
- Python 3.0, 3.1, and 3.2 (2016-10-08)
27+
- Python 3.0 (2016-10-08)
28+
- Python 3.1 (2016-10-08)
29+
- Python 3.2 (2016-10-08)
2830
- Python 3.3 (2017-12-05)
2931
- Python 3.4 (2018-03-19)
3032

tests/bench_bgra2rgb.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# coding: utf-8
2+
"""
3+
2018-03-19.
4+
5+
Maximum screenshots in 1 second by computing BGRA raw values to RGB.
6+
7+
8+
GNU/Linux
9+
pil_frombytes 595
10+
mss_rgb 437
11+
numpy_flip 183
12+
numpy_slice 156
13+
14+
macOS
15+
pil_frombytes 115
16+
mss_rgb 108
17+
numpy_flip 67
18+
numpy_slice 65
19+
20+
Windows
21+
pil_frombytes 294
22+
mss_rgb 261
23+
numpy_flip 124
24+
numpy_slice 115
25+
"""
26+
27+
import time
28+
29+
import numpy
30+
from PIL import Image
31+
32+
import mss
33+
34+
35+
def mss_rgb(im):
36+
return im.rgb
37+
38+
39+
def numpy_flip(im):
40+
frame = numpy.array(im, dtype=numpy.uint8)
41+
return numpy.flip(frame[:, :, :3], 2).tobytes()
42+
43+
44+
def numpy_slice(im):
45+
return numpy.array(im, dtype=numpy.uint8)[..., [2, 1, 0]].tobytes()
46+
47+
48+
def pil_frombytes(im):
49+
return Image.frombytes('RGB', im.size, im.bgra, 'raw', 'BGRX').tobytes()
50+
51+
52+
def benchmark(func):
53+
with mss.mss() as sct:
54+
m = {'top': 0, 'left': 0, 'width': 640, 'height': 480}
55+
count = 0
56+
start = time.time()
57+
58+
while (time.time() - start) <= 1:
59+
frame = func(sct.grab(m)) # noqa
60+
count += 1
61+
62+
print(func.__name__, count)
63+
64+
65+
benchmark(pil_frombytes)
66+
benchmark(mss_rgb)
67+
benchmark(numpy_flip)
68+
benchmark(numpy_slice)

tests/benchmarks.py renamed to tests/bench_general.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
# coding: utf-8
2+
"""
3+
2018-03-19.
4+
5+
Original means MSS 3.1.2.
6+
Patched means MSS 3.2.0.
7+
8+
9+
GNU/Linux Original Patched Gain %
10+
grab 2618 2738 +4.58
11+
access_rgb 1083 1128 +4.15
12+
output 324 322 ------
13+
save 320 319 ------
14+
15+
macOS
16+
grab 524 526 ------
17+
access_rgb 396 406 +2.52
18+
output 194 195 ------
19+
save 193 194 ------
20+
21+
Windows
22+
grab 1280 2498 +95.16
23+
access_rgb 574 712 +24.04
24+
output 139 188 +35.25
25+
"""
26+
127
from time import time
228
import mss
329
import mss.tools

tests/test_third_party.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,17 @@ def test_pil_bgra(sct):
5454
box = {'top': 0, 'left': 0, 'width': width, 'height': height}
5555
sct_img = sct.grab(box)
5656

57-
img = Image.frombytes('RGBX', sct_img.size, sct_img.bgra)
58-
assert img.mode == 'RGBX'
57+
img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
58+
assert img.mode == 'RGB'
5959
assert img.size == sct_img.size
6060

61+
for x in range(width):
62+
for y in range(height):
63+
assert img.getpixel((x, y)) == sct_img.pixel(x, y)
64+
65+
img.save('box-bgra.png')
66+
assert os.path.isfile('box-bgra.png')
67+
6168

6269
@pytest.mark.skipif(
6370
Image is None,

0 commit comments

Comments
 (0)