Created
November 30, 2021 09:14
-
-
Save smittytone/b62f3d4203b49f08a4cc39ecf1ebb348 to your computer and use it in GitHub Desktop.
PicoSystem Sprite 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
#!/usr/bin/env python3 | |
""" | |
ps-convert 1.0.0 | |
================ | |
PicoSystem Sprite Converter | |
@authors Dan Malec, modified and named by Tony Smith | |
@copyright 2021 | |
@licence MIT | |
@requirements PIL | |
""" | |
from PIL import Image | |
import sys | |
try: | |
file_name = sys.argv[1] | |
except IndexError: | |
print("Usage:") | |
print(f" {sys.argv[0]} <file_name>") | |
sys.exit() | |
image = Image.open(file_name) | |
pixel_width = image.size[0] | |
pixel_height = image.size[1] | |
digits = 0 | |
for row in range(pixel_height): | |
for col in range(pixel_width): | |
r, g, b, a = image.getpixel((col, row)) | |
scaled_r = int(float(r) / 255.0 * 15.0) | |
scaled_g = int(float(g) / 255.0 * 15.0) | |
scaled_b = int(float(b) / 255.0 * 15.0) | |
scaled_a = int(float(a) / 255.0 * 15.0) | |
pixel = (scaled_g << 12) | (scaled_b << 8) | (scaled_a << 4) | scaled_r | |
print(f"0x{pixel:04X},", end="") | |
digits += 1 | |
if digits >= 32: | |
print("") | |
digits = 0 | |
else: | |
print(" ", end="") | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment