Creating colour palettes using scikit-learn, NumPy & matplotlib

 

Last week I spent a day creating image colour palettes and tutorials using python and matplotlib. I'd wanted to do this project for a while now and it turned out to be quite fun. It was also a good opportunity to brush up my python skills. The tutorials are based on Adrian Price–Whelan's Urban Goggles hack and existing python notebook. The code takes any image format (.jpg, .png etc. ) and creates corresponding colour palettes by performing a k-means clustering analysis in HSV or RGB pixel space.  I've fleshed out some of the steps in the original ipython notebooks, added links to relevant documentation, and links to similar tutorials. 

The main python packages used are scikit-learnNumPy  and matplotlib. As an IDL, Fortran and IRAF veteran, python is still somewhat new to me. I'm still trying to wrap my head around how  data array manipulation differs between programming language, and how python's clustering algorithms work. The tutorials are available on GitHub and nbviewer.

Tutorial 1

Creating Color Palettes from Project Apollo images – Jupyter Notebook

AS07-11-2027: Apollo 7 Hasselblad image from film magazine 11/P - Earth Orbit. Source: Project Apollo Archive. Top palette: HSV clustering. Bottom palette: RGB clustering.

AS11-40-5868: Apollo 11 Hasselblad image from film magazine 40/S - EVA. Source: Project Apollo Archive. Top palette: HSV clustering. Bottom palette: RGB clustering.

AS07-4-1584: Apollo 7 Hasselblad image from film magazine 4/N - Earth Orbit.  Source: Project Apollo Archive. Top palette: HSV clustering. Bottom palette: RGB clustering.              

AS07-7-1776: Apollo 7 Hasselblad image from film magazine 7/S - Earth Orbit. Source: Project Apollo Archive. Top palette: HSV clustering. Bottom palette: RGB clustering.

The Great Barrier Reef: stretches for 1,400 miles along the coast of Queensland, Australia in the Coral Sea. In this image you can see tidal channels cutting through unnamed reefs.
Source: Planet Labs. Top palette: HSV clustering. Bottom palette: RGB clustering.                                                                                                                                   

Water from the Caucasus Mountains: feeds these large-scale farms in Stavrapol Krai, Russia. The region’s temperate climate supports grape and grain crops. Source: Planet Labs. Top palette: HSV clustering. Bottom palette: RGB clustering.                                                                                                                     

The city of Lethbridge, Alberta: is surrounded by agricultural fields. Near infrared data (in which healthy vegetation appears bright red) collected by RapidEye satellites helps farmers improve crop yields. Source: Planet Labs. Top palette: HSV clustering. Bottom palette: RGB clustering.

Venetian Lagoon‘s Lido Inlet: A breakwater, an artificial island, and a series of massive sluice gates as seen by a RapidEye satellite. These structures are part of the MOSE project—a massive engineering project designed to protect the city of Venice from rising seasonal floodwaters. Source: Planet Labs

more examples

I was curious know how well the algorithm would go with photos of my own ceramics. I'm not sure it worked as well as for the Apollo and Planet Lab images. Perhaps the pixel binning wasn't quite right? This will need further investigation.

Ceramic vases: Top palette: HSV clustering. Bottom palette: RGB clustering.

Everyday bowls: Top palette: HSV clustering. Bottom palette: RGB clustering.

Everyday bowls: Top palette: HSV clustering. Bottom palette: RGB clustering.

Work in progress: Bisqued vases at Mercator Ceramics School. Top palette: HSV clustering. Bottom palette: RGB clustering.