Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created September 17, 2018 16:51
Show Gist options
  • Select an option

  • Save ImadDabbura/30bf36136221b1ef3ea99586a825c486 to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/30bf36136221b1ef3ea99586a825c486 to your computer and use it in GitHub Desktop.
# Read the image
img = imread('images/my_image.jpg')
img_size = img.shape
# Reshape it to be 2-dimension
X = img.reshape(img_size[0] * img_size[1], img_size[2])
# Run the Kmeans algorithm
km = KMeans(n_clusters=30)
km.fit(X)
# Use the centroids to compress the image
X_compressed = km.cluster_centers_[km.labels_]
X_compressed = np.clip(X_compressed.astype('uint8'), 0, 255)
# Reshape X_recovered to have the same dimension as the original image 128 * 128 * 3
X_compressed = X_compressed.reshape(img_size[0], img_size[1], img_size[2])
# Plot the original and the compressed image next to each other
fig, ax = plt.subplots(1, 2, figsize = (12, 8))
ax[0].imshow(img)
ax[0].set_title('Original Image')
ax[1].imshow(X_compressed)
ax[1].set_title('Compressed Image with 30 colors')
for ax in fig.axes:
ax.axis('off')
plt.tight_layout();
@Sajan-poudel

Sajan-poudel commented Apr 1, 2020

Copy link
Copy Markdown

where did that cluster_centers_ came from in line 13.

X_compressed = km.cluster_centers_[km.labels_]

@ImadDabbura

Copy link
Copy Markdown
Author

It is an attribute for "km" object. You can look at Sklearn's doc for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment