Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created July 12, 2026 22:31
Show Gist options
  • Select an option

  • Save jweinst1/af7b5351c84cd4b9e3cc57347f444a06 to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/af7b5351c84cd4b9e3cc57347f444a06 to your computer and use it in GitHub Desktop.
dynamically extracts pixel art to make C++ byte array to a palette
from PIL import Image
import sys
BOT_GEN_CODE = "\n};\n"
def determine_color_set(imgobj, color_prof):
# This makes a new palette based on the colors used in the image itself, no altering
pxset = {}
num = 0
img = imgobj.convert(color_prof)
width, height = img.size
for y in range(height):
for x in range(width):
my_pix = img.getpixel((x, y))
if my_pix not in pxset:
pxset[my_pix] = str(num)
num += 1
return pxset
def per_square_print(imgobj, colset, x, y, xsize, ysize, varname):
my_var_string = f"static constexpr uint8_t {varname}[] = {{\n"
my_gen_strings = []
for k in range(y, y + ysize):
my_row = []
for j in range(x, x + xsize):
my_row.append(colset[(img.getpixel((j, k)))])
my_gen_strings.append(", ".join(my_row))
full_string = my_var_string + ",\n".join(my_gen_strings) + BOT_GEN_CODE
print(full_string)
# Open the PNG image
image_path = sys.argv[1]
cpp_var_name = sys.argv[2]
def get_piece_name(subname):
return subname + "_" + cpp_var_name
with Image.open(image_path) as img:
pixset = determine_color_set(img, "RGBA")
print(pixset)
print(" ")
img = img.convert('RGBA')
width, height = img.size
assert width == 96
assert height == 16
per_square_print(img, pixset, 0, 0, 16, 16, get_piece_name("PAWN"))
per_square_print(img, pixset, 16, 0, 16, 16, get_piece_name("KNIGHT"))
per_square_print(img, pixset, 32, 0, 16, 16, get_piece_name("ROOK"))
per_square_print(img, pixset, 48, 0, 16, 16, get_piece_name("BISHOP"))
per_square_print(img, pixset, 64, 0, 16, 16, get_piece_name("QUEEN"))
per_square_print(img, pixset, 80, 0, 16, 16, get_piece_name("KING"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment