Skip to content

Instantly share code, notes, and snippets.

@Vigowebs
Created April 2, 2023 17:44
Show Gist options
  • Save Vigowebs/7ddf164648ab1d1b0f94c21b49e68370 to your computer and use it in GitHub Desktop.
Save Vigowebs/7ddf164648ab1d1b0f94c21b49e68370 to your computer and use it in GitHub Desktop.
Color Extraction from Image using Python
from PIL import Image
Image.open('sunset.jpg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('sunset.jpg')
w, h, d = tuple(image.shape)
pixels = np.reshape(image, (w * h, d))
from sklearn.cluster import KMeans
n_colors = 10
model = KMeans(n_clusters=n_colors, random_state=42).fit(pixels)
palette = np.uint8(model.cluster_centers_)
plt.imshow([palette])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment