-
-
Save SwitchCraft3/e6a59dab9a04807f5d035e4e72720524 to your computer and use it in GitHub Desktop.
SwitchCraft Peripherals 2DJ converter
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
import argparse | |
import json | |
import sys | |
from PIL import Image | |
parser = argparse.ArgumentParser(description="sc-peripherals 2DJ converter") | |
parser.add_argument("--label", help="Poster label") | |
parser.add_argument("--tooltip", help="Poster tooltip") | |
parser.add_argument("image", help="Source image") | |
parser.add_argument("output", help="Output image (without ext.)") | |
args = parser.parse_args() | |
img = Image.open(args.image) | |
if img.mode != "RGB": | |
img = img.convert("RGB") | |
small = img.resize((128, 128)) | |
reduced = small.quantize(63) | |
palette = [int.from_bytes(bytearray(c), "big") for c in reduced.palette.colors.keys()] | |
pixels = [reduced.getpixel((y, x)) for x in range(128) for y in range(128)] | |
pixels = [1 if p == 0 else p for p in pixels] # 0 is transparent | |
data = { | |
"width": 128, | |
"height": 128, | |
"palette": palette, | |
"pixels": pixels | |
} | |
if args.label: | |
data["label"] = args.label | |
if args.tooltip: | |
data["tooltip"] = args.tooltip | |
with open(args.output + ".2dj", "w") as f: | |
json.dump(data, f, separators=(",", ":")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment