找出图片的像素最多的前200的颜色

from PIL import Image
image_path = "/root/a_lilei/test4/白87.jpg"
path = '/root/a_user/car_color/color.txt'
f = open(path,'r').readlines()
colors = []
colors_rgb=[]
for i in f:
    
    color = i.split('\t')[1].split(' ')
    color = list(map(int,color))
    colors_rgb.append(color)
    label = i.split('\t')[-1].replace('\n','')
    colors.append([color,label])

image = Image.open(image_path)
image_color = image.getcolors(55555)
image_color = sorted(image_color,reverse = True)
num = len(colors_rgb)
for j in range(0,200):
    color_differ=(np.array(colors_rgb)-np.array([list(image_color[j][1])]*num))**2
    differ_sum = list(color_differ.sum(axis = 1))
    differ_min = min(differ_sum)
    color_index = differ_sum.index(differ_min)
    print(colors[color_index],image_color[j][0],image_color[j][1])

color.txt中的文件如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_34496674/article/details/88404603
今日推荐