https://coloravg.run.goorm.io/
import pandas as pd
import math
data = pd.read_csv("color-rgb.csv")
RGB = data.iloc[:, 3:]
RGB = RGB.apply(lambda x: x.astype(str).map(lambda x: int(x, base=16)/255.0))
RGB["v"] = RGB.max(axis=1)
RGB["m"] = RGB.min(axis=1)
RGB["h"] = ""
RGB["s"] = ""
for i in range(len(RGB)):
if RGB["v"][i] == 0:
RGB["s"][i] = 0
RGB["h"][i] = 0
else:
RGB["s"][i] = 1 - (RGB["m"][i] / RGB["v"][i])
if RGB["v"][i] == RGB["R"][i]:
RGB["h"][i] = 60 * (RGB["G"][i] - RGB["B"][i]) / (RGB["v"][i] - RGB["m"][i])
elif RGB["v"][i] == RGB["G"][i]:
RGB["h"][i] = 120 + (60 * (RGB["B"][i] - RGB["R"][i])) / (RGB["v"][i] - RGB["m"][i])
elif RGB["v"][i] == RGB["B"][i]:
RGB["h"][i] = 240 + (60 * (RGB["R"][i] - RGB["G"][i])) / (RGB["v"][i] - RGB["m"][i])
if RGB["h"][i] < 0:
RGB["h"][i] += 360
RGB["h"][i] /= 360
RGB["h"][i] *= 255
RGB["s"] = RGB["s"].apply(lambda x: int(x*255))
RGB["v"] = RGB["v"].apply(lambda x: int(x*255))
RGB = RGB.fillna(0)
RGB["h"] = RGB["h"].apply(lambda x: int(x))
HSV = RGB[["h", "s", "v"]]
HSV["x"] = ""
HSV["y"] = ""
HSV["z"] = ""
for i in range(len(HSV)):
HSV["x"][i] = HSV["s"][i]*math.cos(2*math.pi*HSV["h"][i]/255)
HSV["y"][i] = HSV["s"][i]*math.sin(2*math.pi*HSV["h"][i]/255)
HSV["z"] = HSV["v"]
HSV = pd.concat([data["name"], HSV], axis=1)
print(HSV)
HSV.to_csv("color-hsv-xyz.csv")
from sklearn.cluster import KMeans
import numpy as np
import cv2
import math
import pandas as pd
color = pd.read_csv("HSV.csv")
color_rgb = pd.read_csv("color-rgb.csv")
analysis = pd.read_csv("Analysis.csv", index_col=0)
def avg_color(img):
encoded_img = np.fromstring(img, np.uint8)
image = cv2.imdecode(encoded_img, cv2.IMREAD_COLOR)
image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA)
#image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
image = image.reshape((image.shape[0] * image.shape[1], 3)) #HSV이미지를 색상의 한줄 조합으로 가져오는 부분
k = 4
colorlist_all = []
clt = KMeans(n_clusters=k)
clt.fit(image)
print(image.shape)
i = 0
for center in clt.cluster_centers_[0:4]:
i += 1
print(center)
center[0] = center[0]/255.0
center[1] = center[1]/255.0
center[2] = center[2]/255.0
v = max(center)
m = min(center)
print(v)
if v == 0:
s = 0
h = 0
else:
s = 1 - m/v
if v == center[2]:
h = 60 * (center[1] - center[0]) / (v - m)
elif v == center[1]:
h = 120 + (60 * (center[0] - center[2])) / (v - m)
elif v == center[0]:
h = 240 + (60 * (center[2] - center[1])) / (v - m)
if h < 0:
h += 360
h /= 360
h *= 255
s = s*255
v = v*255
x = s*math.cos(2*math.pi*h/255)*v/255
y = s*math.sin(2*math.pi*h/255)*v/255
z = v
df_min = 1000
for color_c in range(len(color)):
df_c = math.sqrt(
(x-color["x"][color_c])**2 +
(y-color["y"][color_c])**2 +
(z-color["z"][color_c])**2
)
if df_min > df_c:
df_min = df_c
df_min_f = color_c
print(color["name"][df_min_f])
print(color_rgb["RGB"][df_min_f])
colordict = {
"name": color["name"][df_min_f],
"code": color_rgb["RGB"][df_min_f],
"tone": color["tone"][df_min_f],
"base": color["base"][df_min_f]
}
colorlist_all.append(colordict)
return colorlist_all
ex)
면접 떨어지고 확실히 나의 수준을 더욱 알게되어서 열심히 하게된다.!