-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolor_palettes.py
174 lines (136 loc) · 5.27 KB
/
color_palettes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from pathlib import Path
from math import ceil
from functools import partial
from io import BytesIO
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from skimage import color
from sklearn.cluster import KMeans, AgglomerativeClustering
from typing import Union, Callable
def read_image(path: Union[str,Path]) -> np.ndarray:
with open(path,"rb") as f:
return np.array(Image.open(f))
def get_kmeans_centers(img: np.ndarray, nclusters: int) -> np.ndarray:
return KMeans(n_clusters=nclusters).fit(img).cluster_centers_
def get_agglom_centers(img: np.ndarray, nclusters: int) -> np.ndarray:
return AgglomerativeClustering(n_clusters=nclusters).fit(img).cluster_centers_
def rgb2hsv(dat: np.ndarray) -> np.ndarray:
return color.rgb2hsv([dat])[0]
def hsv2rgb(dat: np.ndarray) -> np.ndarray:
return color.hsv2rgb([dat])[0]
def preprocess_image(img: np.ndarray) -> np.ndarray:
return img.reshape((-1,3)).astype("float32") / 255
def preprocess_rgb(img: np.ndarray) -> np.ndarray:
return img.reshape((-1,3)).astype("float32") / 255
def preprocess_hsv(img: np.ndarray) -> np.ndarray:
return rgb2hsv(preprocess_rgb(img))
def plot_image(img: np.ndarray):
plt.figure(figsize=(14,8))
plt.imshow(img)
plt.grid()
plt.axis('off')
plt.show()
def plot_palette(centers: np.ndarray):
plt.figure(figsize=(14,6))
plt.imshow(centers[
np.concatenate([[i] * 100 for i in range(len(centers))]).reshape((-1,10)).T
])
plt.grid()
plt.axis('off')
plt.show()
def make_all_palettes(path: Union[str,Path], nclusters: int = 8, filter_fn: Callable[[np.ndarray],np.ndarray] = None):
# Load the image
img = read_image(path)
# Reshape and set range
rgb_pixels = preprocess_image(img)
# If filter_fn is set, filter pixels
if filter_fn is not None:
rgb_pixels = filter_fn(rgb_pixels)
# Convert RGB to HSV
hsv_pixels = rgb2hsv(rgb_pixels)
# Cluster the pixels
km_rgb_centers = get_kmeans_centers(rgb_pixels,nclusters)
km_hsv_centers = get_kmeans_centers(hsv_pixels,nclusters)
km_hsv_centers = hsv2rgb(km_hsv_centers)
agglom_rgb_centers = get_kmeans_centers(rgb_pixels,nclusters)
agglom_hsv_centers = get_kmeans_centers(hsv_pixels,nclusters)
agglom_hsv_centers = hsv2rgb(agglom_hsv_centers)
# Plot the image
plot_image(img)
# Plot the palette
print("KMeans RGB clustering")
plot_palette(km_rgb_centers)
print("KMeans HSV clustering")
plot_palette(km_hsv_centers)
print("Agglomerative RGB clustering")
plot_palette(agglom_rgb_centers)
print("Agglomerative HSV clustering")
plot_palette(agglom_hsv_centers)
print("\n" + "=" * 100 + "\n")
def filter_pixels(pixels: np.ndarray, low = 0.0, high = 1.0) -> np.ndarray:
pix_mean = pixels.mean(1)
mask = (low <= pix_mean) & (pix_mean <= high)
idx = np.arange(len(pixels))
return pixels[idx[mask]]
def palette_to_img(colors):
"""colors is a 2D array (palette-size, 3-channels)
"""
assert len(colors.shape) == 2
n_colors = len(colors)
w = ceil(n_colors ** 0.75)
h = ceil(n_colors / w)
n_pad = (w * h) - n_colors
if colors.max() <= 1:
c = colors * 255
else:
c = colors
c = np.concatenate((c,255*np.ones((n_colors,1))),1)
c = np.concatenate((c,np.zeros((n_pad,4),int)))
c = c.reshape((h,w,4))
img = Image.fromarray(c.astype("uint8"))
return img.resize((w*200,h*200),Image.NEAREST)
def file_to_img(upload):
with open(upload,"rb") as f:
img = Image.open(f)
return Image.fromarray(np.array(img))
def calc_palette(img: np.ndarray, hsv: bool = False, method: Union["kmeans","agglom"] = "kmeans",
nclusters: int = 5, filter_low: float = 0.0, filter_high: float = 1.0, callback: Callable = None):
"""
"""
if callback: callback(0)
cluster_fn = get_kmeans_centers if method.lower() == "kmeans" else get_agglom_centers
if callback: callback(1)
# Load the image
# img = read_image(path)
if callback: callback(2)
# Reshape and set range
pixels = preprocess_image(img)
if callback: callback(3)
# If filter_fn is set, filter pixels
filter_fn = partial(filter_pixels,low=filter_low,high=filter_high)
pixels = filter_fn(pixels)
if callback: callback(4)
# Convert RGB to HSV
if hsv: pixels = rgb2hsv(pixels)
if callback: callback(5)
# Cluster the pixels
centers = cluster_fn(pixels,nclusters)
if hsv: centers = hsv2rgb(centers)
if callback: callback(9)
return (centers * 255).astype(int)
def calc_palette_to_img(path: Union[str,Path], hsv: bool = False, method: Union["kmeans","agglom"] = "kmeans",
nclusters: int = 5, filter_low: float = 0.0, filter_high: float = 1.0, callback: Callable = None):
"""
"""
assert isinstance(hsv,bool)
assert method in ("kmeans","agglom")
assert isinstance(nclusters,int)
# assert 1 <= nclusters <= 35, nclusters
assert 0 <= filter_low <= 1, filter_low
assert 0 <= filter_high <= 1, filter_high
assert filter_low < filter_high, str(filter_low,filter_high)
palette = calc_palette(path,hsv,method,
nclusters,filter_low,filter_high,callback)
if callback: callback(10)
return palette_to_img(palette)