MeanC=((int)(Mean_R/count),(int)(Mean_G/count),(int)(Mean_B/count))
return Me
番茄的颜色提取到了,那么和什么做比对呢?
当然是口红的数据,文摘菌这儿用到了 5 个品牌,分别是圣罗兰、香奈儿可可小姐、迪奥、美宝莲、纪梵希,共 17 个系列,271 个口红色号。
数据集是一个嵌套的字典数据结构,存为 json 串的形式,里面记录了每个口红品牌系列下不同口红色号的颜色 id、名称、和 16 进制颜色值。
lipstick.json部分数据集展示如下:
{“brands”:[{“name”:“圣罗兰”,“series”:
[{“name”:“莹亮纯魅唇膏”,“lipsticks”:
[{“color”:"#D62352",“id”:“49”,“name”:“撩骚”},
{“color”:"#DC4B41",“id”:“14”,“name”:“一见倾心”},
{“color”:"#B22146",“id”:“05”,“name”:“浮生若梦”},
数据集中存储的 RGB 颜色是 16 进制的字符串形式,需要将其转换成 RGB 值,比较两个颜色相近与否。
实际上是比较 RGB 三个分量维度上的误差,最小的口红输出对应的品牌、系列、色号和 id。
代码如下:
import json
import getcolor
import numpy as np
import lipcolor
#filename = ‘temp.txt’
##write the temp data to file##
def WtoFile(filename,RGB_temp):
num=len(RGB_temp)
with open(filename,‘w’) as f:
for i in range(num):
s = str(RGB_temp[i]).replace(’[’,’’).replace(’]’,’’)
f.write(s)
f.write("\n")
#operate the data #
##save the brand&series&color id&color name to sum_list##
##covert the color #D62352 to RGB_array##
##caculate the RGB difference to RGB_temp and write the value to file##
def data_operate():
with open(‘lipstick.json’, ‘r’, encoding=‘utf-8’) as f:
ret_dic = json.load(f)
#print(ret_dic[‘brands’])
#print(type(ret_dic)) # <class ‘dict’>
#print(ret_dic[‘brands’][0][‘name’])
b_num=len(ret_dic[‘brands’])
#print(b_num)#brands number
s_list=[]
#series brands#
for i in range(len(ret_dic[‘brands’])):
s_num=len(ret_dic[‘brands’][i][‘series’])
s_list.append(s_num)
#print("{0} has {1} series".format((ret_dic[‘brands’][i][‘name’]),(s_list[i])))
#the lipstick color of every brands every series#
#the first loop calculate the total color numbers
sum=0
for b1 in range(b_num):
for s1 in range(s_list[b1]):
brand_name=ret_dic[‘brands’][b1][‘name’]
lip_name=ret_dic[‘brands’][b1][‘series’][s1][‘name’]
color_num=len(ret_dic[‘brands’][b1][‘series’][s1][‘lipsticks’])
sum+=color_num#calculate the total color numbers
#the second loop save the message to a list#
sum_list=np.zeros((sum,4), dtype=(str,8))
value_array=np.zeros((sum,6), dtype=int)
i=0
for b2 in range(b_num):
for s2 in range(s_list[b2]):
brand_name=ret_dic[‘brands’][b2][‘name’]
#print(type(brand_name))
lip_name=ret_dic[‘brands’][b2][‘series’][s2][‘name’]
color_num=len(ret_dic[‘brands’][b2][‘series’][s2][‘lipsticks’])
for c in range(color_num):
color_value=ret_dic[‘brands’][b2][‘series’][s