gbprinter/download.py

56 lines
1.3 KiB
Python
Executable File

#!/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.
-p Panorama mode. Works with print panoramas on the camera.
'''
import twobpp
import sys
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)]
port = serial.Serial(arguments["<port>"], 115200)
runs = 1
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, TILES_PER_LINE, WIDTH, HEIGHT)
if runs != 1:
p = arguments["<path>"].split(".")
image.save(f"{p[0]}-{photo_number}.{p[1]}")
else:
image.save(arguments["<path>"])
port.close()