Add image convertor

This commit is contained in:
Miloslav Číž 2018-09-03 13:43:38 +02:00
parent f46946036b
commit 7eecabd620

32
convert_png.py Normal file
View file

@ -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};")