refactoring. Incomplete panorama mode.

This commit is contained in:
Nihilazo 2019-08-23 12:08:11 +01:00
parent 5cfebaf706
commit 85d9a7cbc8
3 changed files with 15 additions and 10 deletions

View File

@ -10,6 +10,10 @@ import sys
import docopt
from PIL import Image
TILES_PER_LINE = 20
HEIGHT = 144
WIDTH = 160
arguments = docopt(__doc__)
colors = [(0xff, 0xff, 0xff), (0xaa, 0xaa, 0xaa), (0x55,0x55,0x55), (0x00,0x00,0x00)]
@ -27,5 +31,5 @@ for line in l:
d.append(line)
b = [bytes.fromhex(h.strip()) for h in d]
image = twobpp.convert_photo(b, colors)
image = twobpp.convert_photo(b, colors, TILES_PER_LINE, WIDTH, HEIGHT)
image.save(arguments["<output>"])

View File

@ -9,6 +9,7 @@ Usage:
Options:
-b amount Batch Mode, Downloads the specified amount of images at once.
-p Panorama mode. Works with print panoramas on the camera.
'''
import twobpp
@ -17,6 +18,10 @@ import serial
from docopt import docopt
from PIL import Image
TILES_PER_LINE = 20
HEIGHT = 144
WIDTH = 160
arguments = docopt(__doc__)
colors = [(0xff, 0xff, 0xff), (0xaa, 0xaa, 0xaa), (0x55,0x55,0x55), (0x00,0x00,0x00)]
@ -40,7 +45,7 @@ for photo_number in range(int(runs)):
d.append(line)
b = [bytes.fromhex(h.strip().decode()) for h in d]
image = twobpp.convert_photo(b, colors)
image = twobpp.convert_photo(b, colors, TILES_PER_LINE, WIDTH, HEIGHT)
if runs != 1:
p = arguments["<path>"].split(".")
image.save(f"{p[0]}-{photo_number}.{p[1]}")

View File

@ -1,10 +1,6 @@
# 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):
@ -26,22 +22,22 @@ def decode_line(b1, b2):
line.append((hi_bit << 1) | lo_bit)
return line
def convert_photo(byte_seq, colors):
def convert_photo(byte_seq, colors, tiles_per_line, width, height):
"""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):
for t in chunks(tiles, tiles_per_line):
chunk = []
for l in range(8):
line = []
for tile in range(TILES_PER_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 = Image.new(mode='RGB',size=(width,height))
image.putdata(image_data)
return image