-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathppm-enlarger.py
41 lines (33 loc) · 1.06 KB
/
ppm-enlarger.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
repeat = 5
spacing = 2
bgcolor_t = (8,8,8)
with open(sys.argv[1], "rb") as f:
b = f.read()
_firstn = b.index(b"\n")
orig_width, orig_height = map(int, b[_firstn+1:b.index(b"\n", _firstn+1)].split(b" "))
width = orig_width*repeat+orig_width*spacing
height = orig_height*repeat+orig_height*spacing
_s = b"255\n"
_p = b.index(_s) + len(_s)
tmp = bytearray()
for _i in range(orig_width*orig_height):
start = _p + _i*3
end = start + 3
_bytes = b[start:end]
tmp.extend(bytearray(bgcolor_t)*spacing)
tmp.extend(bytearray(_bytes)*repeat)
out = bytearray()
for _w in range(height):
start = _w*width*3
end = start + width*3
_lb = tmp[start:end]
out.extend(bytearray(bgcolor_t)*(width+spacing)*spacing)
out.extend((_lb+bytearray(bgcolor_t)*spacing)*repeat)
height += spacing
width += spacing
out.extend(bytearray(bgcolor_t)*width*spacing)
with open("out-mod.ppm", "wb") as f:
f.write(b"P6\n"+bytes(str(width), encoding='ascii')+b" "+bytes(str(height), encoding='ascii')+b"\n255\n"+out)