From 7eecabd6205d42266894f2f72a09fb95f055925a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 3 Sep 2018 13:43:38 +0200 Subject: [PATCH] Add image convertor --- convert_png.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 convert_png.py 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};") +