Use numpy and imageio in gen_palette.py

This commit is contained in:
Gael-de-Sailly 2018-07-30 22:47:11 +02:00
parent a503e06a77
commit 334e08a69d

49
gen_palette.py Normal file → Executable file
View File

@ -1,12 +1,14 @@
from PIL import Image
from array import array
#!/usr/bin/env python3
import numpy as np
import imageio
from zlib import compress
#palette = Image.new("RGB", (16, 16))
palette_list = []
palette_raw = np.zeros((256,3))
modifiers = [
modifiers = np.array([
(0.25, 0.00),
(0.50, 0.00),
(0.75, 0.00),
@ -17,22 +19,9 @@ modifiers = [
(0.25, 0.50),
(0.25, 0.25),
(0.50, 0.25),
]
])
def add_color(color):
global palette_list
palette_list += list(color)
def variations(color):
for mod in modifiers:
sat = mod[0]
off = mod[1]
r = int((color[0] * sat + off) * 255 + 0.5)
g = int((color[1] * sat + off) * 255 + 0.5)
b = int((color[2] * sat + off) * 255 + 0.5)
add_color((r, g, b))
hues = [
hues = np.array([
(1.00, 0.00, 0.00),
(1.00, 0.25, 0.00),
(1.00, 0.50, 0.00),
@ -57,21 +46,23 @@ hues = [
(1.00, 0.00, 0.75),
(1.00, 0.00, 0.50),
(1.00, 0.00, 0.25),
]
])
i = 0
for hue in hues:
variations(hue)
for sat, off in modifiers:
palette_raw[i] = hue * sat + off
i += 1
for i in range(16):
l = i * 17
add_color((l, l, l))
for g in range(16):
palette_raw[i] = g / 15
i += 1
palette_buffer = bytes(palette_list)
palette = (palette_raw * 255 + 0.5).astype(np.uint8)
print(palette)
palette = Image.frombuffer("RGB", (16, 16), palette_buffer)
palette = palette.transpose(Image.FLIP_TOP_BOTTOM)
palette.save("palette.png")
imageio.imwrite("palette.png", palette.reshape(16,16,3))
color_list = open("colors.dat", "wb")
color_list.write(compress(palette_buffer))
color_list.write(compress(bytes(palette)))
color_list.close()