Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd52984

Browse files
authored
MSS: Possibility to get the whole PNG raw bytes
1 parent e9dee83 commit dd52984

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dev 2017/xx/xx
66
- removed support for Python 3.3
77
- CI: build the documentation
88
- doc: improvements and fixes (fix #37)
9+
- MSS: possibility to get the whole PNG raw bytes
910

1011
3.1.1 2017/11/27
1112
- MSS: add the 'mss' entry point

docs/source/api.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ Methods
110110

111111
.. module:: mss.tools
112112

113-
.. method:: to_png(data, size, output)
113+
.. method:: to_png(data, size, output=None)
114114

115115
:param bytes data: RGBRGB...RGB data.
116116
:param tuple size: The (width, height) pair.
117117
:param str output: output's file name.
118118
:raises ScreenShotError: On error when writing ``data`` to ``output``.
119119

120120
Dump data to the image file. Pure Python PNG implementation.
121+
If ``output`` is ``None``, create no file but return the whole PNG data.
121122

122123
.. versionadded:: 3.0.0
123124

mss/tools.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import zlib
99

1010

11-
def to_png(data, size, output):
12-
# type: (bytes, Tuple[int, int], str) -> None
11+
def to_png(data, size, output=None):
12+
# type: (bytes, Tuple[int, int], Optional[str]) -> Union[None, bytes]
1313
"""
14-
Dump data to a PNG file.
14+
Dump data to a PNG file. If `output` is `None`, create no file but return
15+
the whole PNG data.
1516
1617
:param bytes data: RGBRGB...RGB data.
1718
:param tuple size: The (width, height) pair.
@@ -43,6 +44,10 @@ def to_png(data, size, output):
4344
iend[3] = struct.pack('>I', zlib.crc32(iend[1]) & 0xffffffff)
4445
iend[0] = struct.pack('>I', len(iend[2]))
4546

47+
if not output:
48+
# Returns raw bytes of the whole PNG data
49+
return magic + b''.join(ihdr + idat + iend)
50+
4651
with open(output, 'wb') as fileh:
4752
fileh.write(magic)
4853
fileh.write(b''.join(ihdr))

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
'Programming Language :: Python :: 2',
3434
'Programming Language :: Python :: 2.7',
3535
'Programming Language :: Python :: 3',
36-
'Programming Language :: Python :: 3.3',
3736
'Programming Language :: Python :: 3.4',
3837
'Programming Language :: Python :: 3.5',
3938
'Programming Language :: Python :: 3.6',

tests/test_tools.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# coding: utf-8
2+
3+
import hashlib
4+
import os.path
5+
6+
from mss.tools import to_png
7+
8+
9+
WIDTH = 10
10+
HEIGHT = 10
11+
MD5SUM = '055e615b74167c9bdfea16a00539450c'
12+
13+
14+
def test_output_file():
15+
data = b'rgb' * WIDTH * HEIGHT
16+
output = '{}x{}.png'.format(WIDTH, HEIGHT)
17+
to_png(data, (WIDTH, HEIGHT), output=output)
18+
19+
assert os.path.isfile(output)
20+
with open(output, 'rb') as png:
21+
assert hashlib.md5(png.read()).hexdigest() == MD5SUM
22+
23+
24+
def test_output_raw_bytes():
25+
data = b'rgb' * WIDTH * HEIGHT
26+
raw = to_png(data, (WIDTH, HEIGHT))
27+
assert hashlib.md5(raw).hexdigest() == MD5SUM

0 commit comments

Comments
 (0)