Last active
April 16, 2019 04:18
-
-
Save doegox/042c400e19394e6bca4dae70067d4dc3 to your computer and use it in GitHub Desktop.
Image to badge shitty script, with script helper for uploading it directly on the badge via USB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# TROOPERS2019 | |
# @doegox | |
# The script will resize the image and convert it to BW image if needed but | |
# I would advise to do it yourself first with e.g. Gimp for a better control | |
# of the aspect ration and the BW threshold. | |
# Target: 296x128, black & white (no grey) | |
# Shall we display the result locally? | |
PREVIEW_ONLY=False | |
import sys | |
from PIL import Image | |
DST_WIDTH=296 | |
DST_HEIGHT=128 | |
def data2img(d): | |
img = Image.new( '1', (DST_WIDTH, DST_HEIGHT), "white") | |
pixels = img.load() | |
for i in range(img.size[0]//8): | |
for j in range(img.size[1]): | |
for k in range(8): | |
pixels[i*8+k,j] = (not (d[int(i+j*DST_WIDTH//8)]>>(7-k) & 1)) | |
return img | |
def img2data(img): | |
if img.size[0] != DST_WIDTH or img.size[1] != DST_HEIGHT: | |
print("Resizing image...") | |
img = img.resize((DST_WIDTH,DST_HEIGHT)) | |
if img.mode != '1': | |
print("Converting image...") | |
img = img.convert("1") | |
d=[0]*(img.size[0]//8*img.size[1]) | |
pixels = img.load() | |
for i in range(img.size[0]//8): | |
for j in range(img.size[1]): | |
for k in range(8): | |
d[int(i+j*DST_WIDTH//8)] |= (not pixels[i*8+k,j]) << (7-k) | |
return d | |
if __name__ == '__main__': | |
if len(sys.argv) >= 2 and sys.argv[1] == "-p": | |
PREVIEW_ONLY=True | |
sys.argv.pop(0) | |
if len(sys.argv) < 2: | |
print(f""" | |
Usages: | |
- preview: | |
{sys.argv[0]} -p myfile.png | |
- online: requires the Troopers badge server which was present on site during the event. | |
{sys.argv[0]} myfile.png | |
It will return a large table. | |
Visit https://badge.troopers.de/name/ and copy/paste the table in its Local Storage | |
then upload it to the canvas. | |
- offline: requires the file config.json of the badge. | |
{sys.argv[0]} myfile.png config.json | |
It will update config.json. | |
""") | |
sys.exit(1) | |
img = Image.open(sys.argv[1]) | |
d = img2data(img) | |
if PREVIEW_ONLY: | |
data2img(d).show() | |
sys.exit(0) | |
if len(sys.argv) < 3: | |
print(d) | |
else: | |
import json | |
import base64 | |
with open(sys.argv[2], "r") as f: | |
j = json.load(f) | |
# Old image: | |
#data2img(base64.b64decode(j['IMAGE'])).show() | |
# New image: | |
j['IMAGE'] = base64.b64encode(bytes(d)).decode('utf8') | |
with open(sys.argv[2], "w") as f: | |
json.dump(j, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# TROOPERS2019 | |
# @doegox | |
# The script will resize the image and convert it to BW image if needed but | |
# I would advise to do it yourself first with e.g. Gimp for a better control | |
# of the aspect ration and the BW threshold. | |
# Target: 296x128, black & white (no grey) | |
export AMPY_PORT=${AMPY_PORT:-/dev/ttyUSB0} | |
export AMPY_BAUD=115200 | |
IMG=$1 | |
if ! which ampy >dev>null 2>&1; then | |
echo "\"ampy\" not found, please install it, e.g.:" | |
echo "python3 -m pip install --user adafruit-ampy" | |
exit 1 | |
fi | |
if [ "$IMG" == "" ]; then | |
echo "Usage: $0 myfile.png" | |
echo "It requires the badge to be connected" | |
echo "Adapt AMPY_PORT if needed, depending on your OS" | |
exit 1 | |
fi | |
# Connect the badge, "reset" and keep "start" pressed till the "name" screen appears | |
echo "Retrieving config.json..." | |
ampy get config.json > config.json | |
./troopers2019_img2badge.py $IMG config.json | |
echo "Uploading config.json..." | |
ampy put config.json | |
echo "Done. Reset your badge and keep \"start\" pressed" |
For MacOS and Windows you probably need to install these drivers:
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
and then point AMPY_PORT
to the correct UART port
Nice job, thx! I collected my first steps with the badge here https://github.com/ThetaGamma/Troopers19_Badge and referenced you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Little scripts to upload an image to the badge over USB.
Example:
./troopers2019_img2badge_offline.sh nahuelito.png
Requirements: