Add image convertor
This commit is contained in:
parent
f46946036b
commit
7eecabd620
1 changed files with 32 additions and 0 deletions
32
convert_png.py
Normal file
32
convert_png.py
Normal 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};")
|
||||
|
Loading…
Reference in a new issue