Skip to content

Instantly share code, notes, and snippets.

@hornc
Created July 31, 2025 06:02
Show Gist options
  • Save hornc/9ce622c0281efd21636c955770a48cd1 to your computer and use it in GitHub Desktop.
Save hornc/9ce622c0281efd21636c955770a48cd1 to your computer and use it in GitHub Desktop.
pixel art to numeric text grid
#!/usr/bin/env python3
import sys
from PIL import Image
# opens a limited palette
# pixel art image and outputs
# a 0-9 digit grid
# + palette
if __name__ == '__main__':
fname = sys.argv[1]
img = Image.open(fname)
w, h = img.size
colors = {}
for y in range(h):
for x in range(w):
r,g,b,a = img.getpixel((x,y))
col = f'{r},{g},{b}'
if a == 0:
print('0', end='')
elif col in colors:
print(colors[col], end='')
else:
colors[col] = len(colors) + 1
print(colors[col], end='')
print()
print('\n', colors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment