So I have a predefined color palette which I would like to color the picture with. I was researching how to do this and found this so far.
import scipy.spatial as sp
import matplotlib.pyplot as plt
import cv2
import numpy
avg = image.open("sourcefile.png")
#Color palette I would ike to use
main_colors = [(100, 100, 100),
(204, 0, 0),
(0, 174, 0),
(102, 51, 153),
(255, 102, 0),
(0, 0, 155)]
h, w, bpp = numpy.shape(avg)
# Change colors of each pixel
for py in range(0, h) :
for px in range(0, w):
input_color = avg[py][px][0], avg[py][px][1], avg[py][px][2]
tree = sp.KDTree(main_colors)
distance, result = tree.query(input_color)
nearest_color = main_colors[result]
avg[py][px][0] = nearest_color[0]
avg[py][px][1] = nearest_color[1]
avg[py][px][2] = nearest_color[2]
# show image
plt.figure()
plt.axis("off")
plt.imshow(avg)
I believe, I am really close to the solution, however, I can't seem to it. Can someone help me debug this? I keep getting this and I don't know how to solve this
File "/Average Pictures.py", line 22, in <module>
input_color = avg[py][px][0], avg[py][px][1], avg[py][px][2]
TypeError: 'Image' object is not subscriptable
After Looking at @Quang Hongs comment I changed the code too:
import scipy.spatial as sp
import matplotlib.pyplot as plt
import cv2
import numpy
...
...
...
# Blending loaded Images
avg = Image.open(imlist[0])
for i in range(1, N):
img = Image.open(imlist[i])
avg = Image.blend(avg, img, 1.0 / float(i + 1))
avg1 = Image.new("RGB", avg.size)
avg1 = asarray(avg1)
main_colors =numpy.array([(100, 100, 100),
(204, 0, 0),
(0, 174, 0),
(102, 51, 153),
(255, 102, 0),
(0, 0, 155),
])
main_colors = numpy.array(main_colors)
dist_mat = sp.distance_matrix(avg1.reshape(-1,3), main_colors)
color_idx = dist_mat.argmax(axis=1)
nearest_colors = main_colors[color_idx].reshape(avg1.shape)
fig, axes = plt.subplots(1, 2)
axes[0].imshow(avg) # original image
axes[1].imshow(nearest_colors) # nearest_color
plt.show()
The problem now however ist, that it doesn't output a correct image.
Can someone help ? Sorry for being an absolute noob.