forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
50 lines (40 loc) · 1.48 KB
/
tools.py
File metadata and controls
50 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# coding: utf-8
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
"""
import struct
import zlib
def to_png(data, size, output):
# type: (bytes, Tuple[int, int], str) -> None
"""
Dump data to a PNG file.
:param bytes data: RGBRGB...RGB data.
:param tuple size: The (width, height) pair.
:param str output: Output file name.
"""
width, height = size
line = width * 3
png_filter = struct.pack('>B', 0)
scanlines = b''.join(
[png_filter + data[y * line:y * line + line]
for y in range(height)])
magic = struct.pack('>8B', 137, 80, 78, 71, 13, 10, 26, 10)
# Header: size, marker, data, CRC32
ihdr = [b'', b'IHDR', b'', b'']
ihdr[2] = struct.pack('>2I5B', width, height, 8, 2, 0, 0, 0)
ihdr[3] = struct.pack('>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff)
ihdr[0] = struct.pack('>I', len(ihdr[2]))
# Data: size, marker, data, CRC32
idat = [b'', b'IDAT', zlib.compress(scanlines), b'']
idat[3] = struct.pack('>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff)
idat[0] = struct.pack('>I', len(idat[2]))
# Footer: size, marker, None, CRC32
iend = [b'', b'IEND', b'', b'']
iend[3] = struct.pack('>I', zlib.crc32(iend[1]) & 0xffffffff)
iend[0] = struct.pack('>I', len(iend[2]))
with open(output, 'wb') as fileh:
fileh.write(magic)
fileh.write(b''.join(ihdr))
fileh.write(b''.join(idat))
fileh.write(b''.join(iend))