Spaces:
Runtime error
Runtime error
Commit
·
8350a42
1
Parent(s):
da291f3
Update sketch_helper.py
Browse files- sketch_helper.py +10 -12
sketch_helper.py
CHANGED
|
@@ -6,20 +6,18 @@ from skimage.color import lab2rgb
|
|
| 6 |
from sklearn.cluster import KMeans
|
| 7 |
|
| 8 |
def color_quantization(image, n_colors):
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
n_bins = int(np.ceil(np.sqrt(unique_colors.shape[0])))
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(
|
| 15 |
-
colors = kmeans.cluster_centers_
|
| 16 |
|
| 17 |
-
# Replace each pixel with
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
|
| 24 |
|
| 25 |
def get_high_freq_colors(image):
|
|
|
|
| 6 |
from sklearn.cluster import KMeans
|
| 7 |
|
| 8 |
def color_quantization(image, n_colors):
|
| 9 |
+
# Reshape the image into a 2D array of pixels
|
| 10 |
+
pixels = image.reshape(-1, 3)
|
|
|
|
| 11 |
|
| 12 |
+
# Fit k-means clustering algorithm to the pixel data
|
| 13 |
+
kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(pixels)
|
|
|
|
| 14 |
|
| 15 |
+
# Replace each pixel with its nearest cluster center
|
| 16 |
+
labels = kmeans.predict(pixels)
|
| 17 |
+
colors = kmeans.cluster_centers_.astype(np.uint8)
|
| 18 |
+
quantized_image = colors[labels].reshape(image.shape)
|
| 19 |
+
|
| 20 |
+
return quantized_image
|
| 21 |
|
| 22 |
|
| 23 |
def get_high_freq_colors(image):
|