Я попытался вычислить функцию CIE76 Delta-E для каждого пикселя, чтобы получить ближайший цвет.Python не мой лучший язык, поэтому вы можете задать другой вопрос, чтобы оптимизировать код, если он работает так, как вы ожидаете.
Я в основном конвертирую входное изображение и палитру в цветовое пространство Lab, а затем вычисляю дельту CIE76-E значение возводится в квадрат от каждого пикселя к каждой из записей палитры и принимает ближайший.
#!/usr/bin/env python3
import numpy as np
from PIL import Image
from skimage import color
def CIE76DeltaE2(Lab1,Lab2):
"""Returns the square of the CIE76 Delta-E colour distance between 2 lab colours"""
return (Lab2[0]-Lab1[0])*(Lab2[0]-Lab1[0]) + (Lab2[1]-Lab1[1])*(Lab2[1]-Lab1[1]) + (Lab2[2]-Lab1[2])*(Lab2[2]-Lab1[2])
def NearestPaletteIndex(Lab,palLab):
"""Return index of entry in palette that is nearest the given colour"""
NearestIndex = 0
NearestDist = CIE76DeltaE2(Lab,palLab[0,0])
for e in range(1,palLab.shape[0]):
dist = CIE76DeltaE2(Lab,palLab[e,0])
if dist < NearestDist:
NearestDist = dist
NearestIndex = e
return NearestIndex
palette = [
0,0,0,
0,0,255,
15,29,15,
26,141,52,
41,41,41,
65,105,225,
85,11,18,
128,0,128,
135,206,236,
144,238,144,
159,30,81,
165,42,42,
166,141,95,
169,169,169,
173,216,230,
211,211,211,
230,208,122,
245,245,220,
247,214,193,
255,0,0,
255,165,0,
255,192,203,
255,255,0,
255,255,255
] + [0,] * 232 * 3
# Load the source image as numpy array and convert to Lab colorspace
imnp = np.array(Image.open('lion.png').convert('RGB'))
imLab = color.rgb2lab(imnp)
h,w = imLab.shape[:2]
# Load palette as numpy array, truncate unused palette entries, and convert to Lab colourspace
palnp = np.array(palette,dtype=np.uint8).reshape(256,1,3)[:24,:]
palLab = color.rgb2lab(palnp)
# Make numpy array for output image
resnp = np.empty((h,w), dtype=np.uint8)
# Iterate over pixels, replacing each with the nearest palette entry
for y in range(0, h):
for x in range(0, w):
resnp[y, x] = NearestPaletteIndex(imLab[y,x], palLab)
# Create output image from indices, whack a palette in and save
resim = Image.fromarray(resnp, mode='P')
resim.putpalette(palette)
resim.save('result.png')
Я получаю это:
Кажется, немного быстрее и лаконичнее использовать функцию scipy.spatial.distance
cdist()
:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
from skimage import color
from scipy.spatial.distance import cdist
palette = [
0,0,0,
0,0,255,
15,29,15,
26,141,52,
41,41,41,
65,105,225,
85,11,18,
128,0,128,
135,206,236,
144,238,144,
159,30,81,
165,42,42,
166,141,95,
169,169,169,
173,216,230,
211,211,211,
230,208,122,
245,245,220,
247,214,193,
255,0,0,
255,165,0,
255,192,203,
255,255,0,
255,255,255
] + [0,] * 232 * 3
# Load the source image as numpy array and convert to Lab colorspace
imnp = np.array(Image.open('lion.png').convert('RGB'))
h,w = imnp.shape[:2]
imLab = color.rgb2lab(imnp).reshape((h*w,3))
# Load palette as numpy array, truncate unused palette entries, and convert to Lab colourspace
palnp = np.array(palette,dtype=np.uint8).reshape(256,1,3)[:24,:]
palLab = color.rgb2lab(palnp).reshape(24,3)
# Make numpy array for output image
resnp = np.empty(h*w, dtype=np.uint8)
# Iterate over pixels, replacing each with the nearest palette entry
x = 0
for L in imLab:
resnp[x] = cdist(palLab, L.reshape(1,3), metric='seuclidean').argmin()
x = x +1
# Create output image from indices, whack the palette in and save
resim = Image.fromarray(resnp.reshape(h,w), mode='P')
resim.putpalette(palette)
resim.save('result.png')