1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +00:00
trezor-firmware/tools/png2toi

65 lines
1.6 KiB
Plaintext
Raw Normal View History

2016-03-24 22:00:38 +00:00
#!/usr/bin/env python3
from PIL import Image
import sys
import struct
import zlib
if len(sys.argv) < 2:
2016-03-28 16:30:53 +00:00
print('Usage png2toi image.png [mode]')
2016-03-24 22:00:38 +00:00
sys.exit(1)
ifn = sys.argv[1]
2016-03-28 16:30:53 +00:00
gray = False
if len(sys.argv) >= 3 and sys.argv[2] == 'g':
gray = True
2016-03-24 22:00:38 +00:00
if not ifn.endswith('.png'):
print('Must provide PNG file')
sys.exit(2)
im = Image.open(ifn)
w, h = im.size
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
2016-03-28 16:30:53 +00:00
if not gray:
if not im.mode == 'RGB':
print('PNG file must use RGB mode')
sys.exit(3)
else:
if not im.mode == 'L':
print('PNG file must use grayscale mode')
sys.exit(3)
if w % 2 > 0:
print('PNG file must have width divisible by 2')
sys.exit(4)
2016-03-24 22:00:38 +00:00
pix = im.load()
ofn = '%s.toi' % ifn[:-4]
with open(ofn, 'wb') as f:
2016-03-28 16:30:53 +00:00
if not gray:
f.write(bytes('TOIa', 'ascii'))
else:
f.write(bytes('TOIg', 'ascii'))
2016-03-24 22:00:38 +00:00
f.write(struct.pack('>HH', w, h))
data = bytes()
2016-03-28 16:30:53 +00:00
if not gray:
for j in range(h):
for i in range(w):
r, g, b = pix[i, j]
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
data += struct.pack('>H', c)
else:
for j in range(h):
for i in range(w // 2):
l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j]
c = (l1 & 0xF0) | (l2 >> 4)
data += struct.pack('>B', c)
2016-03-24 22:00:38 +00:00
z = zlib.compressobj(level=9, wbits=10)
zdata = z.compress(data) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))