refactoring, downloader program, new features

This commit is contained in:
Nihilazo 2019-08-22 21:34:18 +01:00
parent 1dc94e24fb
commit 079a46d54c
3 changed files with 114 additions and 1 deletions

31
convert.py Normal file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
'''
Gameboy camera image converter.
Usage:
convert.py <input> <output>
'''
import twobpp
import sys
import docopt
from PIL import Image
arguments = docopt(__doc__)
colors = [(0xff, 0xff, 0xff), (0xaa, 0xaa, 0xaa), (0x55,0x55,0x55), (0x00,0x00,0x00)]
f = open(arguments["<input>"], "r")
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]
image = twobpp.convert_photo(b, colors)
image.save(arguments["<output>"])

50
download.py Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python
'''
Gameboy camera downloader.
Downloads images from the gameboy camera to your PC.
Usage:
./download.py <port> <path>
./download.py -b amount <port> <path>
Options:
-b amount Batch Mode, Downloads the specified amount of images at once.
'''
import twobpp
import sys
import serial
from docopt import docopt
from PIL import Image
arguments = docopt(__doc__)
colors = [(0xff, 0xff, 0xff), (0xaa, 0xaa, 0xaa), (0x55,0x55,0x55), (0x00,0x00,0x00)]
port = serial.Serial(arguments["<port>"], 115200)
runs = 0
if arguments['-b'] != None:
runs = arguments['-b']
for photo_number in range(int(runs)):
d = []
while True:
line = port.readline()
if line.startswith(b"#"):
if line.startswith(b"# F"):
break
elif line.startswith(b"!"):
pass
else:
d.append(line)
b = [bytes.fromhex(h.strip().decode()) for h in d]
image = twobpp.convert_photo(b, colors)
if arguments['-b'] != 0:
p = arguments["<path>"].split(".")
image.save(f"{p[0]}-{photo_number}.{p[1]}")
else:
image.save(arguments["<path>"])
port.close()

View File

@ -1,15 +1,47 @@
# 2bpp.py - functions for the gameboy 2bpp graphics format.
# twobpp.py - functions for the gameboy 2bpp graphics format and gameboy camera
from PIL import Image
TILES_PER_LINE = 20
HEIGHT = 144
WIDTH = 160
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def decode_tile(tile):
"""Decodes an 8x8 2bpp gameboy tile, returns a 2d list containing the data"""
d = []
for i in range(0,16,2):
d.append(decode_line(tile[i], tile[i+1]))
return d
def decode_line(b1, b2):
"""Decodes an 8-pixel line of 2bpp gameboy graphics from two bytes"""
line = []
for i in range(8):
hi_bit = (b2 >> (7-i)) & 1;
lo_bit = (b1 >> (7-i)) & 1;
line.append((hi_bit << 1) | lo_bit)
return line
def convert_photo(byte_seq, colors):
"""converts the sequence of bytes from a gameboy camera image into a pillow image."""
tiles = [decode_tile(l) for l in byte_seq]
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)
return image