gbprinter/main.py

50 lines
1.1 KiB
Python

'''
Converter of gameboy camera images to PNGs. Probably awful.
Gameboy camera images are saved in the format of the mofosyne printer emulator.
'''
#!/usr/bin/env/python
import json
import twobpp
from PIL import Image
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
TILES_PER_LINE = 20
HEIGHT = 144
WIDTH = 160
colors = [(0xff, 0xff, 0xff), (0xaa, 0xaa, 0xaa), (0x55,0x55,0x55), (0x00,0x00,0x00)]
l = f.readlines()
d = []
for line in l:
if line.startswith("#"):
pass
elif line.startswith("!"):
pass
else:
d.append(line)
b = [bytes.fromhex(h.strip()) for h in d]
tiles = [twobpp.decode_tile(l) for l in b]
tile_image = []
for t in chunks(tiles, TILES_PER_LINE):
chunk = []
for l in range(8):
line = []
for tile in range(TILES_PER_LINE):
line += t[tile][l]
chunk += line
tile_image += chunk
image_data = [colors[x] for x in tile_image]
image = Image.new(mode='RGB',size=(WIDTH,HEIGHT))
image.putdata(image_data)