diff --git a/convert_png.py b/convert_png.py new file mode 100644 index 0000000..fffa452 --- /dev/null +++ b/convert_png.py @@ -0,0 +1,32 @@ +# Convert png image to 256 palette image for Pokitto. +# +# author: Miloslav Ciz +# license: CC0 + +import sys +from PIL import Image + +def rgbToIndex(r, g, b): + return (r & 0b00000111) | ((g & 0b00000111) << 3) | ((b & 0b00000011) << 6) + +image = Image.open(sys.argv[1]) +pixels = image.load() + +sys.stdout.write("const unsigned char image[] =\n{ ") +sys.stdout.write(str(image.size[0]) + ", " + str(image.size[1]) + " // width, height") + +count = 0 + +for y in range(image.size[1]): + for x in range(image.size[0]): + if count % 14 == 0: + sys.stdout.write("\n "); + + p = pixels[x,y] + + sys.stdout.write(",0x{:02x}".format(rgbToIndex(p[0] / 32, p[1] / 32, p[2] / 85))) + + count += 1 + +print("\n};") +