Python WebColor没有定义的颜色名称

2024-06-02 08:14:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用webcolors获取十六进制颜色值的名称

我为每个规范运行了一个映像css2css21css3html4

每次运行时,我都会出现以下错误:

'#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
.
.

这是Example image

下面是我用来产生错误的代码:

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()

结论是,产生的十六进制值不存在于当前规范中

因此,是否可以选择与当前十六进制值最接近的可用十六进制值

或任何其他解决方案

问候


Tags: nonameinimportformatimgcolorint
1条回答
网友
1楼 · 发布于 2024-06-02 08:14:11

我们可以得到@fmw42建议的最接近的图像。(非常感谢,先生,我感谢您的建议。)

  • compute the rmse difference between your test color and the color values of all of the color names. Take the one with the smallest rmse. You will need to convert from hex to r,g,b to do the rmse computation.

对于每个图像名称、十六进制值,将每个十六进制值转换为rgb

for img_clr, img_hex in webcolors.CSS3_NAMES_TO_HEX.items():
    cur_clr = webcolors.hex_to_rgb(img_hex)

计算均方根误差(RMSE)

rmse = np.sqrt(mean_squared_error(c, cur_clr))

其中c变量是我们当前的rgb

将其存储在列表中

rms_lst.append(rmse)

获取最近的图像名称索引

closest_color = rms_lst.index(min(rms_lst))

获取图像的名称

nm = list(webcolors.CSS3_NAMES_TO_HEX.items())[closest_color][0]

例如:

enter image description here

代码:


import cv2
import sys
import glob
import webcolors
import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.metrics import mean_squared_error
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))
        rms_lst = []
        for img_clr, img_hex in webcolors.CSS3_NAMES_TO_HEX.items():
            cur_clr = webcolors.hex_to_rgb(img_hex)
            rmse = np.sqrt(mean_squared_error(c, cur_clr))
            rms_lst.append(rmse)

        closest_color = rms_lst.index(min(rms_lst))

        nm = list(webcolors.CSS3_NAMES_TO_HEX.items())[closest_color][0]
    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()

相关问题 更多 >