Spaces:
Runtime error
Runtime error
Commit
·
5fef8b2
1
Parent(s):
b65a245
Update sketch_helper.py
Browse files- sketch_helper.py +34 -19
sketch_helper.py
CHANGED
@@ -6,27 +6,42 @@ from skimage.color import lab2rgb
|
|
6 |
from sklearn.cluster import KMeans
|
7 |
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
freqs = [c[0] for c in sorted_colors]
|
14 |
-
mean_freq = sum(freqs) / len(freqs)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
colors
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def create_binary_matrix(img_arr, target_color):
|
32 |
# Create mask of pixels with target color
|
|
|
6 |
from sklearn.cluster import KMeans
|
7 |
|
8 |
|
9 |
+
def count_high_freq_colors(image):
|
10 |
+
im = image.getcolors(maxcolors=1024*1024)
|
11 |
+
sorted_colors = sorted(im, key=lambda x: x[0], reverse=True)
|
|
|
|
|
|
|
12 |
|
13 |
+
freqs = [c[0] for c in sorted_colors]
|
14 |
+
mean_freq = sum(freqs) / len(freqs)
|
15 |
|
16 |
+
high_freq_colors = [c for c in sorted_colors if c[0] > max(2, mean_freq*1.25)]
|
17 |
+
return high_freq_colors
|
18 |
+
|
19 |
+
def get_high_freq_colors(image, similarity_threshold=30):
|
20 |
+
image_copy = image.copy()
|
21 |
+
high_freq_colors = count_high_freq_colors(image)
|
22 |
+
# Check for similar colors and replace the lower frequency color with the higher frequency color in the image
|
23 |
+
for i, (freq1, color1) in enumerate(high_freq_colors):
|
24 |
+
for j, (freq2, color2) in enumerate(high_freq_colors):
|
25 |
+
if (color_distance(color1, color2) < similarity_threshold) or (color_distance(color1, opaque_color_on_white(color2, 0.5)) < 5):
|
26 |
+
if(freq2 > freq1):
|
27 |
+
replace_color(image_copy, color1, color2)
|
28 |
+
|
29 |
+
high_freq_colors = count_high_freq_colors(image_copy)
|
30 |
+
print(high_freq_colors)
|
31 |
+
return [high_freq_colors, image_copy]
|
32 |
+
|
33 |
+
def color_quantization(image, color_frequency_list):
|
34 |
+
# Extract the color values from the frequency list
|
35 |
+
color_values = [color for _, color in color_frequency_list]
|
36 |
+
|
37 |
+
# Replace the colors that are not in the frequency list with white
|
38 |
+
mask = np.ones(image.shape[:2], dtype=bool)
|
39 |
+
for color in color_values:
|
40 |
+
color_mask = np.all(image == color, axis=2)
|
41 |
+
mask = np.logical_and(mask, np.logical_not(color_mask))
|
42 |
+
image[mask] = (255, 255, 255)
|
43 |
+
|
44 |
+
return image
|
45 |
|
46 |
def create_binary_matrix(img_arr, target_color):
|
47 |
# Create mask of pixels with target color
|