-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain .py
66 lines (59 loc) · 1.76 KB
/
main .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import extcolors
from rembg import remove
from PIL import Image
def text_to_dict() -> dict:
path = "./colors.txt"
with open(path, 'r') as f:
data = f.readlines()
colors = {}
for arr in data:
arr = arr.replace('\n', '')
arr = arr.replace('[', '(')
arr = arr.replace(']', ')')
idx = arr.find(')')
rgb_str = arr[idx + 2:]
rgb = arr[1:idx]
rgb = rgb.replace(',', '').split(' ')
rgb_tup = int(rgb[0]), int(rgb[1]), int(rgb[2])
colors[rgb_tup] = rgb_str
return colors
def get_me_a_name(color: tuple) -> str:
r, g, b = color
remember = 1000
res = ''
for k, v in text_to_dict().items():
r_delta = abs(k[0] - r)
g_delta = abs(k[1] - g)
b_delta = abs(k[2] - b)
tmp = r_delta + g_delta + b_delta
if tmp < remember:
remember = tmp
res = v
return res
def dominante_color(color_arr: list) -> str:
dicti = {}
for color in color_arr:
print(color)
name = get_me_a_name(color[0])
pixel_count = int(color[1])
if name in dicti:
dicti[name] += pixel_count
else:
dicti[name] = pixel_count
max = 0
res = ''
print(dicti)
for k, v in dicti.items():
if v > max:
res = k
max = v
return res
def extract_color(in_path: str)-> str:
# out_path = in_path[:in_path.find('.')] + '_nobg' + '.png'
input_img = Image.open(in_path)
output_img = remove(input_img)
color, pixelcount = extcolors.extract_from_image(output_img)
return dominante_color(color)
# output_img.save(out_path)
in_path = "C:/Users/USER/Desktop/hmgoepprod.webp"
print(extract_color(in_path))