From 85d9a7cbc8d95d43b3bc9429c5e50425f039dccf Mon Sep 17 00:00:00 2001 From: Nihilazo Date: Fri, 23 Aug 2019 12:08:11 +0100 Subject: [PATCH] refactoring. Incomplete panorama mode. --- convert.py | 6 +++++- download.py | 7 ++++++- twobpp.py | 12 ++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/convert.py b/convert.py index f8f1085..0a50560 100644 --- a/convert.py +++ b/convert.py @@ -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[""]) diff --git a/download.py b/download.py index d1451c7..0b0ecf7 100755 --- a/download.py +++ b/download.py @@ -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[""].split(".") image.save(f"{p[0]}-{photo_number}.{p[1]}") diff --git a/twobpp.py b/twobpp.py index 5152992..af1706e 100644 --- a/twobpp.py +++ b/twobpp.py @@ -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