Created
April 2, 2023 17:44
-
-
Save Vigowebs/7ddf164648ab1d1b0f94c21b49e68370 to your computer and use it in GitHub Desktop.
Color Extraction from Image using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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