Skip to content

Commit bea462a

Browse files
authored
Version 3.0.0
Also: - MSS: refactor, introducing the ScreenShot class and Numpy array interface support - More pythonic API (breaking the compatibility with version < 3.0.0) - More examples - Fixes BoboTiG#16.
1 parent 07ab78c commit bea462a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1129
-849
lines changed

.coafile

Lines changed: 0 additions & 6 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ addons:
1414
- xserver-xorg-video-dummy
1515

1616
before_install:
17-
- xpra --xvfb="Xorg +extension RANDR -config `pwd`/tests/dummy.xorg.conf -logfile ${HOME}/.xpra/xorg.log" start :42
17+
- xpra --xvfb="Xorg +extension RANDR -config `pwd`/tests/res/dummy.xorg.conf -logfile ${HOME}/.xpra/xorg.log" start :42
1818

1919
install:
2020
- pip install flake8 pylint

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ History:
22

33
<see Git checkin messages for history>
44

5+
dev 2017/xx/xx
6+
7+
8+
3.0.0 2017/07/06
9+
- big refactor, introducing the ScreenShot class
10+
- add Numpy array interface support
11+
- examples: add OpenCV/Numpy, PIL pixels, FPS
12+
513
2.0.22 2017/04/29
614
- new contributors: David Becker, redodo
715
- add an example to capture only a part of the screen

CHANGES.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
3.0.0 (2017-07-06)
2+
==================
3+
4+
base.py
5+
-------
6+
- Added the `ScreenShot` class containing data for a given screen shot (support the Numpy array interface [`ScreenShot.__array_interface__`])
7+
- Add `shot()` method to `MSSBase`. It takes the same arguments as the `save()` method.
8+
- Renamed `get_pixels` to `grab`. It now returns a `ScreenShot` object.
9+
- Moved `to_png` method to `tools.py`. It is now a simple function.
10+
- Removed `enum_display_monitors()` method. Use `monitors` property instead.
11+
- Removed `monitors` attribute. Use `monitors` property instead.
12+
- Removed `width` attribute. Use `ScreenShot.size[0]` attribute or `ScreenShot.width` property instead.
13+
- Removed `height` attribute. Use `ScreenShot.size[1]` attribute or `ScreenShot.height` property instead.
14+
- Removed `image`. Use the `ScreenShot.raw` attribute or `ScreenShot.rgb` property instead.
15+
- Removed `bgra_to_rgb()` method. Use `ScreenShot.rgb` property instead.
16+
17+
darwin.py
18+
---------
19+
- Removed `_crop_width()` method. Screen shots are now using the width setted by the OS (rounded to 16).
20+
21+
exception.py
22+
------------
23+
- Renamed `ScreenshotError` class to `ScreenShotError`
24+
25+
tools.py
26+
--------
27+
- Changed signature of `to_png(data, monitor, output)` to `to_png(data, size, output)` where `size` is a `tuple(width, height)`

README.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ Python MSS
44
.. image:: https://travis-ci.org/BoboTiG/python-mss.svg?branch=dev
55
:target: https://travis-ci.org/BoboTiG/python-mss
66

7+
8+
.. code-block:: python
9+
10+
from mss import mss
11+
12+
13+
# The simplest use, save a screenshot of the 1st monitor
14+
with mss() as sct:
15+
sct.shot()
16+
17+
718
An ultra fast cross-platform multiple screenshots module in pure python using ctypes.
819

920
- **Python 2 & 3** and PEP8 compliant, no dependency;
1021
- very basic, it will grab one screen shot by monitor or a screen shot of all monitors and save it to a PNG file;
1122
- but you can use PIL and benefit from all its formats (or add yours directly);
23+
- integrate well with Numpy and OpenCV;
1224
- it could be easily embedded into games and other softwares which require fast and plateforme optimized methods to grab screenshots;
1325
- get the `source code on GitHub <https://github.com/BoboTiG/python-mss>`_;
26+
- learn with a `bunch of examples <https://github.com/BoboTiG/python-mss/tree/master/examples>`_;
1427
- you can `report a bug <https://github.com/BoboTiG/python-mss/issues>`_;
1528
- and there is a `complete, and beautiful, documentation <https://python-mss.readthedocs.io>`_ :)
1629
- **MSS** stands for Multiple ScreenShots;
17-
- insanely fast, did I say it?
1830

1931

2032
Installation

docs/source/api.rst

Lines changed: 109 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ MSS API
55
Classes
66
=======
77

8-
``MSSBase`` is the parent's class for every OS implementation.
9-
10-
118
GNU/Linux
129
---------
1310

@@ -21,12 +18,12 @@ GNU/Linux
2118

2219
GNU/Linux initializations.
2320

21+
.. method:: grab(monitor) -> ScreenShot
2422

25-
.. method:: get_pixels(monitor) -> bytes
26-
27-
:exception ScreenshotError: When color depth is not 32 (rare).
23+
:exception ScreenShotError: When color depth is not 32 (rare).
24+
:rtype: ScreenShot
2825

29-
See :attr:`mss.base.MSSBase.get_pixels` for details.
26+
See :attr:`mss.base.MSSBase.grab` for details.
3027

3128

3229
Methods
@@ -36,104 +33,159 @@ Methods
3633

3734
.. class:: MSSBase
3835

39-
.. method:: bgra_to_rgb(raw) -> bytes
36+
The parent's class for every OS implementation.
4037

41-
:param bytearray raw: raw data containing BGRA values.
38+
.. method:: grab(monitor) -> ScreenShot
4239

43-
It converts pixels values from BGRA to RGB.
44-
This is the method called to populate :attr:`image` into :attr:`get_pixels`.
40+
:param dict monitor: monitor's informations.
41+
:exception NotImplementedError: Subclasses need to implement this.
42+
:rtype: ScreenShot
4543

44+
Retrieve screen pixels for a given monitor.
4645

47-
.. method:: enum_display_monitors(force=False) -> list(dict)
46+
.. method:: save(mon=1, output='screenshot.png', callback=None) -> generator
4847

49-
:param bool force: force rescan of monitors informations.
50-
:exception NotImplementedError: Subclasses need to implement this.
48+
:param int mon: the monitor's number.
49+
:param str output: the output's file name. ``%d``, if present, will be replaced by the monitor number.
50+
:param callable callback: callback called before saving the screenshot to a file. Takes the ``output`` argument as parameter.
51+
:return generator: Created file(s).
5152

52-
Get positions of one or more monitors.
53-
If the monitor has rotation, you have to deal with it inside this method.
53+
Grab a screenshot and save it to a file.
5454

55-
This method has to fill :attr:`monitors` with all informations and use it as a cache:
55+
.. method:: shot() -> str
5656

57-
- ``monitors[0]`` is a dict of all monitors together;
58-
- ``monitors[N]`` is a dict of the monitor N (with N > 0).
57+
:return str: The created file.
5958

60-
Each monitor is a dict with::
59+
Helper to save the screenshot of the first monitor, by default.
60+
You can pass the same arguments as for ``save``.
6161

62-
{
63-
'left': the x-coordinate of the upper-left corner,
64-
'top': the y-coordinate of the upper-left corner,
65-
'width': the width,
66-
'height': the height
67-
}
62+
.. class:: ScreenShot
6863

64+
Screen shot object.
6965

70-
.. method:: get_pixels(monitor) -> bytes
66+
.. note::
7167

72-
:param dict monitor: monitor's informations generated by :attr:`enum_display_monitors()`.
73-
:exception NotImplementedError: Subclasses need to implement this.
68+
A better name would have been *Image*, but to prevent collisions
69+
with ``PIL.Image``, it has been decided to use *ScreenShot*.
7470

75-
Retrieve screen pixels for a given monitor.
76-
This method has to define :attr:`width` and :attr:`height`.
77-
It stocks pixels data into :attr:`image` (RGB) and returns it.
71+
.. classmethod:: from_size(cls, data, width, height) -> ScreenShot
7872

73+
:param bytearray data: raw BGRA pixels retrieved by ctype
74+
OS independent implementations.
75+
:param int width: the monitor's width.
76+
:param int height: the monitor's height.
77+
:rtype: ScreenShot
7978

80-
.. method:: save(mon=0, output='monitor-%d.png', callback=None) -> generator
79+
Instanciate a new class given only screenshot's data and size.
8180

82-
:param int mon: the monitor's number.
83-
:param str output: the output's file name. ``%d``, if present, will be replaced by the monitor number.
84-
:param callable callback: callback called before saving the screenshot to a file. Takes the ``output`` argument as parameter.
81+
.. method:: pixels(coord_x, coord_y) -> Tuple[int, int, int]
82+
83+
:param int coord_x: The x coordinate.
84+
:param int coord_y: The y coordinate.
85+
:rtype: Tuple[int, int, int]
8586

86-
Grab a screenshot and save it to a file. This is a generator which returns created files.
87+
Get the pixel value at the given position.
8788

89+
.. module:: mss.tools
8890

89-
.. method:: to_png(data, output) -> None
91+
.. method:: to_png(data, size, output) -> None
9092

91-
:param bytes data: raw pixels (RGBRGB...RGB) fom :attr:`get_pixels()`.
92-
:param str output: output's file name.
93-
:exception ScreenshotError: On error when writing ``data`` to ``output``.
93+
:param bytes data: RGBRGB...RGB data.
94+
:param tuple size: The (width, height) pair.
95+
:param str output: output's file name.
96+
:exception ScreenShotError: On error when writing ``data`` to ``output``.
9497

95-
Dump data to the image file. Pure Python PNG implementation.
98+
Dump data to the image file. Pure Python PNG implementation.
9699

97100

98-
Attributes
101+
Properties
99102
==========
100103

101104
.. class:: MSSBase
102105

103-
.. attribute:: image
106+
.. attribute:: monitors
104107

105-
:getter: Raw pixels of a monitor.
106-
:setter: See :attr:`get_pixels`.
107-
:type: bytes
108+
Positions of all monitors.
109+
If the monitor has rotation, you have to deal with it
110+
inside this method.
108111

112+
This method has to fill ``self._monitors`` with all informations
113+
and use it as a cache:
109114

110-
.. attribute:: monitors
115+
- ``self._monitors[0]`` is a dict of all monitors together
116+
- ``self._monitors[N]`` is a dict of the monitor N (with N > 0)
111117

112-
:getter: The list of all monitors.
113-
:setter: See :attr:`enum_display_monitors()`.
114-
:type: list(dict)
118+
Each monitor is a dict with:
115119

120+
- ``left``: the x-coordinate of the upper-left corner
121+
- ``top``: the y-coordinate of the upper-left corner
122+
- ``width``: the width
123+
- ``height``: the height
116124

117-
.. attribute:: width
125+
:type: List[Dict[str, int]]
126+
127+
.. class:: ScreenShot
128+
129+
.. attribute:: __array_interface__()
130+
131+
Numpy array interface support. It uses raw data in BGRA form.
132+
133+
:type: Dict[str, Any]
118134

119-
:getter: Width of a monitor.
120-
:setter: See :attr:`get_pixels()`.
135+
.. attribute:: pos
136+
137+
The screen shot's coodinates.
138+
139+
:type: NamedTuple
140+
141+
.. attribute:: top
142+
143+
The screen shot's top coodinate.
144+
145+
:type: int
146+
147+
.. attribute:: left
148+
149+
The screen shot's left coodinate.
121150
:type: int
122151

152+
.. attribute:: size
153+
154+
The screen shot's size.
155+
156+
:type: NamedTuple
157+
158+
.. attribute:: width
159+
160+
The screen shot's width.
161+
162+
:type: int
123163

124164
.. attribute:: height
125165

126-
:getter: Height of a monitor.
127-
:setter: See :attr:`get_pixels()`.
166+
The screen shot's height.
167+
128168
:type: int
129169

170+
.. attribute:: pixels
171+
172+
List of RGB tuples.
173+
174+
:type: List[Tuple[int, int, int]]
175+
176+
.. attribute:: rgb
177+
178+
Computed RGB values from the BGRA raw pixels.
179+
180+
:type: bytes
181+
130182

131183
Exception
132184
=========
133185

134186
.. module:: mss.exception
135187

136-
.. exception:: ScreenshotError
188+
.. exception:: ScreenShotError
137189

138190
Base class for MSS exceptions.
139191

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# built documents.
6060
#
6161
# The short X.Y version.
62-
version = '2.0.21'
62+
version = '3.0.0'
6363
# The full version, including alpha/beta/rc tags.
6464
release = 'latest'
6565

0 commit comments

Comments
 (0)