ii-pix/precompute_distance.py
2021-01-10 20:10:32 +00:00

39 lines
926 B
Python

import bz2
import pickle
import dither
import colour.difference
import numpy as np
COLOURS = 256
def rgb_to_lab(rgb: np.ndarray):
srgb = np.clip(
dither.linear_to_srgb_array(rgb.astype(np.float32) / 255), 0.0,
1.0)
xyz = colour.sRGB_to_XYZ(srgb)
return colour.XYZ_to_Lab(xyz)
def nearest_colours():
diffs = np.empty((COLOURS ** 3, 16), dtype=np.float32)
all_rgb = np.array(tuple(np.ndindex(COLOURS, COLOURS, COLOURS)),
dtype=np.uint8)
all_lab = rgb_to_lab(all_rgb)
for i, palette_rgb in dither.RGB.items():
print(i)
palette_lab = rgb_to_lab(palette_rgb)
diffs[:, i] = colour.difference.delta_E_CIE2000(all_lab, palette_lab)
norm = np.max(diffs)
print(norm)
return (diffs / norm * 255).astype(np.uint8)
#return diffs
n = nearest_colours()
with bz2.open("nearest2.pickle.bz2", "wb") as f:
pickle.dump(n, f)