This is a quick project I’ve had in my mind for a while now. I’ve drawing and painting on and off for about a decade now. My style and preferences have changed quite a bit over the years. I’ve often wondered if I have a “style” per se? I definitely love to try a bunch of different mediums and will never have a color coordinated Instagram grid. So, I was curious if I could take a look at the illustrations I’ve done over the last year and see what the colors look like and if I can use any machine learning techniques to find my “average palette.”
There are a few ways to go about this, but the general idea is that we first want to find a way to feed our method an image and it tells out of all colors in a picture which are the most prominent. For this particular example, we’ll be using an algorithm called K-Means. The general idea of this model is that it tries to group data points based on how similar they are to each other. We tell the model to arbitrarily pick K number of clusters, it’ll then find centroid for each cluster and try to assign data points to each one.
I’ll be using the OpenCV to read the images and scikit-learn for the K-Means method. I won’t be going over how to pick the correct number of K, you can search up the elbow method on your own time.
import cv2 as cv import numpy as np import matplotlib.pyplot as plt import PIL from sklearn.cluster import KMeans import matplotlib as mpl mpl.rcParams['figure.dpi']= 300 def read_img(path): dim = (400, 600) img = cv.imread(path) img = cv.cvtColor(img, cv.COLOR_BGR2RGB) # resize image img = cv.resize(img, dim, interpolation = cv.INTER_AREA) return img img_1 = read_img("photos/1.jpeg")
Note that I am resizing all my images as they all must be the same size. Now, we can call our K-Means method and fit our model.
kmeans = KMeans(n_clusters=6) img_fit = kmeans.fit(img_1.reshape(-1,3)) #reshape to 3 columns, RGB values colors_img = img_fit.cluster_centers_
Note that we need to reshape the data to 3 columns for us to get the proper format for RGB values. The .cluster_centers_ method will then give us the cluster center (average value of color group) post model fitting.
See below a couple examples of the illustration and the average color groups corresponding to it. Swipe left and right to see color comparisons.




I repeated this exercise for about 30 different illustrations and then found the cluster of clusters. And this is the palette it came up with!


I mean, based on the examples above and looking into the colors I gravitate to… there are a lot of pinks and greens and bright colors, so I dig it?