Building a color palette generator in Python
Procreate 5X has this really cool feature that allows you to generate the color palette of a provided image. For those of us who don’t have an iPad (and enjoy spontaneous coding projects), here’s one way you could make your own color palette generator in python.
As an illustration enthusiast, I thought this feature of Procreate was awesome! Not only can an artist break down color palettes from photo references really easily, but one could also consolidate the palettes of their own finished works, ‘study’ the choices of their favorite artists, etc. I mean… I’d probably use the feature this way.
Only, one problem…
I am a Samsung Galaxy Tab user and have never had the privilege of using Procreate — an iOS exclusive. I’m sure there are plenty of color palette generator options out there, but I decided to create my own for fun over a weekend. Here’s how I went about it.
Let’s define a simple objective to get going — given a path to an image, generate its color palette, and slap it on top of the original image. like so:
Let’s Get Started
Now I know what you’re thinking — “can we get to the code already?” Sure why not. Here’s what I used for this:
- Google Colab
- Any public image URL
Set up your Google Colab
To get started, head over to Google Colab and create a new notebook. If you are new to Google Colab, watch this video for a quick intro. I will be providing the code necessary for each cell in the steps ahead.
1. Python Modules
In our first code cell, we need to install/import a couple of modules for use in our functions:
2. Parent function
In our second code cell, we define our top-level function. The study_image() function will orchestrate our process. We will need to define each of the helper functions defined.
3. Helper functions
In our third code cell, we define our helper functions. Let’s talk about them.
- fetch_image(image_path), grabs our image via HTTP request, and stores it in local memory.
- extract_colors(img), extracts the most frequently appearing colors from our image up to a defined limit and based on a tolerance threshold. This returns an array of tuples of type ‘(color.rgb, color.count)’
- render_color_palette(colors), takes the extracted colors array and uses it to generate a grid of colors on a canvas that we can plot.
- overlay_palette(img, color_palette), takes the original image and rendered color palette and creates a new image using matplotlib.
4. Execution cell
We simply need to call our study_image() function and pass in a valid image URL in our final cell.
Run the Colab
Once we have defined the cells above, select Runtime>Run All from the Colab menu options. If you get an error (which may sometimes happen on the first run) select Runtime>Restart and Run All.
Then sit back and watch the magic happen!
You should have a generated image with the color palette overlaid on top. You can right-click on it and save it if you’d like!
Since this first-pass implementation, I have added some extra bells and whistles, including automatic Google Drive uploads for each image and the generated color asset files.
Should I give this little side project a front-end next? Build a larger system that keeps track of these palettes?
Who knows, maybe I’ll expand on this and turn it into a series. We’ll see!