T.Masuda
commited on
Commit
•
1e2f65e
1
Parent(s):
bb2a853
create app
Browse files- app.py +96 -0
- color.py +36 -0
- color_wheel.py +146 -0
- examples/example1.jpg +0 -0
- examples/example2.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import ImageEnhance
|
3 |
+
from color import Color
|
4 |
+
from color_wheel import ColorWheel
|
5 |
+
|
6 |
+
def create_color_table(color):
|
7 |
+
sb = []
|
8 |
+
sb.append('''<!DOCTYPE html>
|
9 |
+
<html lang="ja" xmlns="http://www.w3.org/1999/xhtml">
|
10 |
+
<head>
|
11 |
+
<meta charset="utf-8" />
|
12 |
+
<title></title>
|
13 |
+
<style>
|
14 |
+
.container {display:grid;grid-template-columns:64px 64px 64px 64px 64px 64px 64px 64px 64px 64px 64px 64px}
|
15 |
+
.container > div {padding:3px; color:white; font-size:9pt; height: 32px}
|
16 |
+
</style>
|
17 |
+
</head>
|
18 |
+
<body>
|
19 |
+
''')
|
20 |
+
cwt = ColorWheel(color)
|
21 |
+
colors = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
|
22 |
+
for h in cwt.hue:
|
23 |
+
cw = ColorWheel(Color.from_rgb(h.r, h.g, h.b))
|
24 |
+
for i, t in enumerate(cw.tone15):
|
25 |
+
colors[i].append(t)
|
26 |
+
|
27 |
+
for hue in colors:
|
28 |
+
sb.append(' <div class="container">\n')
|
29 |
+
for h in hue:
|
30 |
+
sb.append(' <div style="background-color:rgb({},{},{})">#{:02x}{:02x}{:02x}</div>\n'.format(h.r, h.g, h.b, h.r, h.g, h.b))
|
31 |
+
sb.append(' </div>\n')
|
32 |
+
sb.append(''' </body >
|
33 |
+
</html>''')
|
34 |
+
html = ''.join(sb)
|
35 |
+
return html
|
36 |
+
|
37 |
+
def chunks(list, n):
|
38 |
+
c = []
|
39 |
+
for i in range(0, len(list), n):
|
40 |
+
c.append(list[i:i + n])
|
41 |
+
return c
|
42 |
+
|
43 |
+
def calc_score(colors, colorset):
|
44 |
+
s = []
|
45 |
+
srccolors = colors.copy()[0:32]
|
46 |
+
for c in colorset:
|
47 |
+
cd = []
|
48 |
+
for color in srccolors:
|
49 |
+
cd.append((color.r - c.r) ** 2 + (color.g - c.g) ** 2 + (color.b - c.b) ** 2)
|
50 |
+
i = min(range(len(cd)), key=cd.__getitem__)
|
51 |
+
s.append(cd[i])
|
52 |
+
srccolors.pop(i)
|
53 |
+
return sum(s)
|
54 |
+
|
55 |
+
def handv(c):
|
56 |
+
cw = ColorWheel.rgb_to_hsv(c.r / 255.0, c.g / 255.0, c.b / 255.0)
|
57 |
+
return cw[1] + cw[2]
|
58 |
+
|
59 |
+
def process_image(image, sf=0.0, cf=0.0):
|
60 |
+
if image is None:
|
61 |
+
return None
|
62 |
+
|
63 |
+
e = ImageEnhance.Color(image)
|
64 |
+
img = e.enhance(cf + 1.0)
|
65 |
+
|
66 |
+
imglim = img.quantize(256)
|
67 |
+
palette = list(chunks(imglim.getpalette(), 3))
|
68 |
+
colors = []
|
69 |
+
for p in palette:
|
70 |
+
r, g, b = p
|
71 |
+
color = Color(255, r, g, b)
|
72 |
+
colors.append(color)
|
73 |
+
colors.sort(key=handv, reverse=True)
|
74 |
+
|
75 |
+
scores = []
|
76 |
+
for c in colors[0:32 + int(sf * 96)]:
|
77 |
+
cw = ColorWheel(c)
|
78 |
+
scores.append(calc_score(colors, cw.hue))
|
79 |
+
i = min(range(len(scores)), key=scores.__getitem__)
|
80 |
+
return create_color_table(colors[i])
|
81 |
+
|
82 |
+
app = gr.Interface(
|
83 |
+
title='Image To Color Wheel',
|
84 |
+
fn=process_image,
|
85 |
+
inputs=[
|
86 |
+
gr.Image(type='pil'),
|
87 |
+
gr.Slider(maximum=1.0, step=0.1, label='sampling factor', value=0.0),
|
88 |
+
gr.Slider(maximum=1.0, step=0.1, label='color factor', value=0.0),
|
89 |
+
],
|
90 |
+
outputs=gr.HTML(),
|
91 |
+
allow_flagging='never',
|
92 |
+
examples=[['examples/example1.jpg'], ['examples/example2.jpg']],
|
93 |
+
#cache_examples=False,
|
94 |
+
concurrency_limit=20,
|
95 |
+
)
|
96 |
+
app.launch()
|
color.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Color:
|
2 |
+
@property
|
3 |
+
def a(self):
|
4 |
+
return self._a
|
5 |
+
|
6 |
+
@property
|
7 |
+
def r(self):
|
8 |
+
return self._r
|
9 |
+
|
10 |
+
@property
|
11 |
+
def g(self):
|
12 |
+
return self._g
|
13 |
+
|
14 |
+
@property
|
15 |
+
def b(self):
|
16 |
+
return self._b
|
17 |
+
|
18 |
+
def __init__(self, a, r, g, b):
|
19 |
+
self._a = a
|
20 |
+
self._r = r
|
21 |
+
self._g = g
|
22 |
+
self._b = b
|
23 |
+
|
24 |
+
@staticmethod
|
25 |
+
def from_argb(a, r, g, b):
|
26 |
+
return Color(a, r, g, b)
|
27 |
+
|
28 |
+
@staticmethod
|
29 |
+
def from_rgb(r, g, b):
|
30 |
+
return Color(0xff, r, g, b)
|
31 |
+
|
32 |
+
def __eq__(self, other):
|
33 |
+
return self.a == other.a and self.r == other.r and self.g == other.g and self.b == other.b
|
34 |
+
|
35 |
+
def __str__(self):
|
36 |
+
return 'ARGB({},{},{},{})'.format(self.a, self.r, self.g, self.b)
|
color_wheel.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
from color import Color
|
3 |
+
|
4 |
+
class ColorWheel:
|
5 |
+
@property
|
6 |
+
def base_color(self):
|
7 |
+
return self._base_color
|
8 |
+
|
9 |
+
@property
|
10 |
+
def hue(self):
|
11 |
+
return [
|
12 |
+
self.base_color, self.add_h(1.0 / 12), self.add_h(2.0 / 12), self.add_h(3.0 / 12), self.add_h(4.0 / 12), self.add_h(5.0 / 12),
|
13 |
+
self.add_h(-6.0 / 12), self.add_h(-5.0 / 12), self.add_h(-4.0 / 12), self.add_h(-3.0 / 12), self.add_h(-2.0 / 12), self.add_h(-1.0 / 12)
|
14 |
+
]
|
15 |
+
|
16 |
+
@property
|
17 |
+
def tone(self):
|
18 |
+
return [self.add_white(-2.0 / 16), self.add_white(-1.0 / 16), self.base_color, self.add_white(1.0 / 16), self.add_white(2.0 / 16)]
|
19 |
+
|
20 |
+
@property
|
21 |
+
def tone15(self):
|
22 |
+
return [self.add_white(-7.0 / 16), self.add_white(-6.0 / 16), self.add_white(-5.0 / 16), self.add_white(-4.0 / 16), self.add_white(-3.0 / 16),
|
23 |
+
self.add_white(-2.0 / 16), self.add_white(-1.0 / 16), self.base_color, self.add_white(1.0 / 16), self.add_white(2.0 / 16),
|
24 |
+
self.add_white(3.0 / 16), self.add_white(4.0 / 16), self.add_white(5.0 / 16), self.add_white(6.0 / 16), self.add_white(7.0 / 16)]
|
25 |
+
|
26 |
+
@property
|
27 |
+
def complementary_colors(self):
|
28 |
+
return [self.base_color, self.add_h(0.5)]
|
29 |
+
|
30 |
+
@property
|
31 |
+
def triadic_colors(self):
|
32 |
+
return [self.add_h(-4.0 / 12), self.base_color, self.add_h(4.0 / 12)]
|
33 |
+
|
34 |
+
@property
|
35 |
+
def split_complementary_colors(self):
|
36 |
+
return [self.add_h(-5.0 / 12), self.base_color, self.add_h(5.0 / 12)]
|
37 |
+
|
38 |
+
@property
|
39 |
+
def analogous_colors(self):
|
40 |
+
return [self.add_h(-2.0 / 12), self.add_h(-1.0 / 12), self.base_color, self.add_h(1.0 / 12), self.add_h(2.0 / 12)]
|
41 |
+
|
42 |
+
def __init__(self, c):
|
43 |
+
self._base_color = c
|
44 |
+
self._r = c.r / 255.0
|
45 |
+
self._g = c.g / 255.0
|
46 |
+
self._b = c.b / 255.0
|
47 |
+
|
48 |
+
@staticmethod
|
49 |
+
def from_hsv(h, s, v):
|
50 |
+
r, g, b = ColorWheel.hsv_to_rgb(h, s, v)
|
51 |
+
c = Color.from_rgb(round(r * 255), round(g * 255), round(b * 255))
|
52 |
+
return ColorWheel(c)
|
53 |
+
|
54 |
+
def add_white(self, value):
|
55 |
+
r, g, b = (self._r + value, self._g + value, self._b + value)
|
56 |
+
r = min(max(r, 0.0), 1.0)
|
57 |
+
g = min(max(g, 0.0), 1.0)
|
58 |
+
b = min(max(b, 0.0), 1.0)
|
59 |
+
return self._from_rgb(r, g, b)
|
60 |
+
|
61 |
+
def add_h(self, value):
|
62 |
+
h, s, v = ColorWheel.rgb_to_hsv(self._r, self._g, self._b)
|
63 |
+
h = (h + value) % 1.0
|
64 |
+
if h < 0.0:
|
65 |
+
h += 1.0
|
66 |
+
r, g, b = ColorWheel.hsv_to_rgb(h, s, v)
|
67 |
+
return self._from_rgb(r, g, b)
|
68 |
+
|
69 |
+
def add_s(self, value):
|
70 |
+
h, s, v = ColorWheel.rgb_to_hsv(self._r, self._g, self._b)
|
71 |
+
s += value
|
72 |
+
s = min(max(s, 0.0), 1.0)
|
73 |
+
r, g, b = ColorWheel.hsv_to_rgb(h, s, v)
|
74 |
+
return self._from_rgb(r, g, b)
|
75 |
+
|
76 |
+
def add_v(self, value):
|
77 |
+
h, s, v = ColorWheel.rgb_to_hsv(self._r, self._g, self._b)
|
78 |
+
v += value
|
79 |
+
v = min(max(v, 0.0), 1.0)
|
80 |
+
r, g, b = ColorWheel.hsv_to_rgb(h, s, v)
|
81 |
+
return self._from_rgb(r, g, b)
|
82 |
+
|
83 |
+
@staticmethod
|
84 |
+
def rgb_to_hsv(r, g, b):
|
85 |
+
if r < 0.0 or r > 1.0:
|
86 |
+
raise ValueError()
|
87 |
+
if g < 0.0 or g > 1.0:
|
88 |
+
raise ValueError()
|
89 |
+
if b < 0.0 or b > 1.0:
|
90 |
+
raise ValueError()
|
91 |
+
cmax = max(r, g, b)
|
92 |
+
cmin = min(r, g, b)
|
93 |
+
h = cmax - cmin
|
94 |
+
if h > 0.0:
|
95 |
+
if cmax == r:
|
96 |
+
h = (g - b) / h
|
97 |
+
if h < 0.0:
|
98 |
+
h += 6.0
|
99 |
+
elif cmax == g:
|
100 |
+
h = 2.0 + (b - r) / h
|
101 |
+
else:
|
102 |
+
h = 4.0 + (r - g) / h
|
103 |
+
h /= 6.0
|
104 |
+
s = cmax - cmin
|
105 |
+
if cmax != 0.0:
|
106 |
+
s /= cmax
|
107 |
+
v = cmax
|
108 |
+
return h, s, v
|
109 |
+
|
110 |
+
@staticmethod
|
111 |
+
def hsv_to_rgb(h, s, v):
|
112 |
+
if h < 0.0 or h > 1.0:
|
113 |
+
raise ValueError()
|
114 |
+
if s < 0.0 or s > 1.0:
|
115 |
+
raise ValueError()
|
116 |
+
if v < 0.0 or v > 1.0:
|
117 |
+
raise ValueError()
|
118 |
+
r = v
|
119 |
+
g = v
|
120 |
+
b = v
|
121 |
+
if s > 0.0:
|
122 |
+
h *= 6.0
|
123 |
+
i = math.floor(h)
|
124 |
+
f = h - i
|
125 |
+
if i == 1:
|
126 |
+
r *= 1 - s * f
|
127 |
+
b *= 1 - s
|
128 |
+
elif i == 2:
|
129 |
+
r *= 1 - s
|
130 |
+
b *= 1 - s * (1 - f)
|
131 |
+
elif i == 3:
|
132 |
+
r *= 1 - s
|
133 |
+
g *= 1 - s * f
|
134 |
+
elif i == 4:
|
135 |
+
r *= 1 - s * (1 - f)
|
136 |
+
g *= 1 - s
|
137 |
+
elif i == 5:
|
138 |
+
g *= 1 - s
|
139 |
+
b *= 1 - s * f
|
140 |
+
else:
|
141 |
+
g *= 1 - s * (1 - f)
|
142 |
+
b *= 1 - s
|
143 |
+
return r, g, b
|
144 |
+
|
145 |
+
def _from_rgb(self, r, g, b):
|
146 |
+
return Color.from_argb(self.base_color.a, round(r * 255), round(g * 255), round(b * 255))
|
examples/example1.jpg
ADDED
examples/example2.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pillow
|