-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (45 loc) · 1.7 KB
/
utils.py
File metadata and controls
59 lines (45 loc) · 1.7 KB
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
import numpy as np
from PIL import Image
from sklearn.cluster import MiniBatchKMeans
import matplotlib.pyplot as plt
def compress_image_kmeans(image, k, batch_size=10000, n_init='auto'):
# Convert image to numpy array
img = np.array(image)
h, w, c = img.shape
pixels = img.reshape(-1, c)
# Apply MiniBatchKMeans with explicit n_init
kmeans = MiniBatchKMeans(n_clusters=k, batch_size=batch_size, n_init=n_init)
kmeans.fit(pixels)
# Replace each pixel by its centroid
new_colors = kmeans.cluster_centers_[kmeans.predict(pixels)]
new_image = new_colors.reshape(h, w, c).astype(np.uint8)
return Image.fromarray(new_image), kmeans
def show_images_side_by_side(image, compressed_image):
# Convert images to numpy arrays
original_array = np.array(image)
compressed_array = np.array(compressed_image)
# Create a figure to display the images side by side
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(original_array)
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('Compressed Image')
plt.imshow(compressed_array)
plt.axis('off')
plt.show()
def show_centroid_colors(kmeans):
colors = kmeans.cluster_centers_
# Create a palette by expanding dimensions of the centroids array
palette = np.expand_dims(colors, axis=0)
# Create a figure to display the colors
plt.figure(figsize=(16, 2))
plt.title('Centroid Colors')
# Set x-ticks to correspond to the centroids
num = np.arange(len(colors))
plt.xticks(num)
plt.yticks([])
# Display the palette
plt.imshow(palette / 255.0) # Normalize color to [0, 1] range
plt.show()