我想使用webcolors获取十六进制颜色值的名称
我为每个规范运行了一个映像css2
、css21
、css3
、html4
每次运行时,我都会出现以下错误:
'#30403c' has no defined color name in css3
'#d4b6c0' has no defined color name in css3
'#8e7766' has no defined color name in css3
'#30403c' has no defined color name in html4
'#d4b6c0' has no defined color name in html4
'#8e7766' has no defined color name in html4
.
.
下面是我用来产生错误的代码:
import cv2
import webcolors
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from collections import Counter
# Source: https://gist.github.com/kb22/f17e59a79d4fcca02188c23cca932be5#file-rgb2hex-py
def rgb2hex(c):
return "#{:02x}{:02x}{:02x}".format(int(c[0]), int(c[1]), int(c[2])) # format(int(c[0]), int(c[1]), int(c[2]))
def hex2name(c):
h_color = '#{:02x}{:02x}{:02x}'.format(int(c[0]), int(c[1]), int(c[2]))
try:
nm = webcolors.hex_to_name(h_color, spec='css3')
except ValueError as v_error:
print("{}".format(v_error))
nm = h_color
return nm
img = cv2.imread("img/landscape.jpg")
img2 = img.reshape(img.shape[0] * img.shape[1], 3)
color = KMeans(n_clusters=3)
lbl = color.fit_predict(img2)
cnt = Counter(lbl)
center_color = color.cluster_centers_
ord_color = [center_color[i] for i in cnt.keys()]
hex_color = [rgb2hex(ord_color[i]) for i in cnt.keys()]
lbl_color = [hex2name(ord_color[i]) for i in cnt.keys()]
plt.pie(cnt.values(), labels=lbl_color, colors=hex_color)
plt.show()
结论是,产生的十六进制值不存在于当前规范中
因此,是否可以选择与当前十六进制值最接近的可用十六进制值
或任何其他解决方案
问候
我们可以得到@fmw42建议的最接近的图像。(非常感谢,先生,我感谢您的建议。)
对于每个图像名称、十六进制值,将每个十六进制值转换为rgb
计算均方根误差(RMSE)
其中
c
变量是我们当前的rgb
值将其存储在列表中
获取最近的图像名称索引
获取图像的名称
例如:
代码:
相关问题 更多 >
编程相关推荐