Anyone who knows me knows I like my floral shirts. With a view to better understanding the kinds of patterns I like (and to perhaps help me create something in the future), I’ve started cataloguing the patterns, textures and colors I encounter—both man-made, and natural—in everyday life.

A large part of a successful print is its color palette, so I’m keen to include this too. The Adobe Kuler app (now Adobe Capture) offers this functionality, but doesn’t record the image alongside the palette. It also seems to have a tendency of dropping some of the more meaningful colors, perhaps in an effort to produce a more pleasing palette.

I tried a couple of different approaches to determining an appropriate color palette myself, starting with a quantize operation before discovering K-means clustering.

Quantize

My first attempt was to quantize the image using ImageMagick:

convert original.jpg -separate -threshold 50% -combine threshold.gif

While this results in a fun image that evokes video games from the 80s, it simply reduces it to an 8-bit palette, loosing all the information we wish to capture:

K-Means

There’s a pretty good discussion on Stack Overflow, which ultimately links to the OpenCV tutorial on K-Means Clustering. While nowhere near as exciting as the results from the quantize operation, this approach results in a palette much closer to that of the original image:

Unfortunately, this seems to result in a somewhat subdued version of the original. Increasing the clusters to 16 can help address this, but those beautiful pinky colours on the tips of the leaves are still missing:

There’s clearly some more work to be done in selecting the more saturated/vivid colors from the clusters.