forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-raw.py
More file actions
71 lines (60 loc) · 2.46 KB
/
test-raw.py
File metadata and controls
71 lines (60 loc) · 2.46 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# coding: utf-8
# Test raw data generated by test-linux.
import mss
import sys
if mss.__version__ < '0.1.0':
from struct import pack
import os.path
import zlib
# Copied from MSS.save_img()
# @deprecated
def save_img(data, width, height, output):
''' Dump data to the image file.
Pure python PNG implementation.
Image represented as RGB tuples, no interlacing.
http://inaps.org/journal/comment-fonctionne-le-png
'''
to_take = (width * 3 + 3) & -4
padding = 0 if to_take % 8 == 0 else (to_take % 8) // 2
scanlines = b''.join(
[b'0' + data[(y * to_take):(y * to_take) + to_take - padding]
for y in range(height)])
magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
# Header: size, marker, data, CRC32
ihdr = [b'', b'IHDR', b'', b'']
ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0)
ihdr[3] = pack(b'>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff)
ihdr[0] = pack(b'>I', len(ihdr[2]))
# Data: size, marker, data, CRC32
idat = [b'', b'IDAT', b'', b'']
idat[2] = zlib.compress(scanlines, 9)
idat[3] = pack(b'>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff)
idat[0] = pack(b'>I', len(idat[2]))
# Footer: size, marker, None, CRC32
iend = [b'', b'IEND', b'', b'']
iend[3] = pack(b'>I', zlib.crc32(iend[1]) & 0xffffffff)
iend[0] = pack(b'>I', len(iend[2]))
with open(output, 'wb') as fileh:
fileh.write(
magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend))
if not os.path.isfile(output):
msg = 'Impossible to write data to file "{0}".'.format(output)
raise ScreenshotError(msg)
if len(sys.argv) < 4:
print('python {0} data.raw width height'.format(sys.argv[0]))
else:
with open(sys.argv[1], 'rb') as fileh:
data = fileh.read()
width = int(sys.argv[2])
height = int(sys.argv[3])
output = '{0}.png'.format(sys.argv[1])
if mss.__version__ < '0.1.0':
print('Outdated version of MSS, please `pip install --upgrade mss`')
save_img(data, width, height, output)
else:
mss = mss.MSS()
mss.save_img(data, width, height, output)
print('File {0} created.'.format(output))
sys.exit(0)
print('Impossible to get contents of "{0}".'.format(sys.argv[1]))