#!/usr/bin/env python3 from PIL import Image import sys import re import struct import zlib import binascii def process_rgb(w, h, pix): data = bytes() 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) return data def process_grayscale(w, h, pix): data = bytes() 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) return data def process_image(ifn): im = Image.open(ifn) w, h = im.size print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode)) if im.mode == 'RGB': print('Detected RGB mode') elif im.mode == 'L': if w % 2 > 0: print('PNG file must have width divisible by 2') return 3 print('Detected GRAYSCALE mode') else: print('Unknown mode:', im.mode) return 4 pix = im.load() if im.mode == 'RGB': ofn = '%s.toif' % ifn[:-4] pixeldata = process_rgb(w, h, pix) else: ofn = '%s.toig' % ifn[:-4] pixeldata = process_grayscale(w, h, pix) z = zlib.compressobj(level=9, wbits=10) zdata = z.compress(pixeldata) + z.flush() zdata = zdata[2:-4] # strip header and checksum toif = b'' if im.mode == 'RGB': toif += b'TOIf' else: toif += b'TOIg' toif += struct.pack('