Skip to content

Instantly share code, notes, and snippets.

@lxpnh98 lxpnh98/
Created Oct 26, 2019

What would you like to do?
# script to get palette of an image (5 main colors)
import sys
from import imread, imshow
from skimage.color import rgb2hsv
from sklearn.cluster import KMeans
# [a] -> [(a, a, a)]
def group(lst, n):
for i in range(0, len(lst), n):
val = lst[i:i+n]
if len(val) == n:
yield tuple(val)
# read image from first argument and convert to HSV
img = rgb2hsv(imread(sys.argv[1]))
# get list of colors
colors = list(group(img.flatten(), 3))
# perform KMeans fit
kmeans = KMeans(n_clusters=5).fit(colors)
# cluster centers represent the palette
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.