Creating colormaps from pictures with tiny bits of ML
Currently the formats are limited to images, but there are plans to improve this to include videos and other media in the future. Open an issue if you're interested in collaborating on this.
The way this works is:
- The image is broken down into RGB color space, with sparse sampling for computational efficiency (controlled via the
npts
argument). - The colors are then (optionally) embedded in 2 dimensions using t-SNE. (See Laurens van der Maaten's page for more information). This is done mainly to get the colors prepped for clustering, (although t-SNE prior to clustering is not always recommended). I find that for this particular application, the resizing of clusters on running tSNE makes things more uniform in color space, and therefore better suited to build palettes. tSNE perplexity and early exaggeration are controlled via the
tsne_perp
andtsne_eggr
parameters. This step can be skipped by turning off theuse_tsne
boolean flag. - The tSNE embedding is then split into
ncolors
discrete clusters, with the clustering implemented through a variety of methods (clustering
= kmeans, GMM, DBSCAN, spectral, optics from sklearn) and the median color in each cluster is calculated. - The colors are then sorted according to different color sorting mechanisms (
sort_type
= rval, hsv, hls, lum, E, step. Seesort_colors()
for more). This can make the palette more uniform in color space, either perceptually, or individual in hue or luminosity. - Finally, excess colors are pruned using the
n_prune
andprune_metric
arguments. This essentially can be used to filter out dominant background colors etc. that might otherwise swamp the palette. A combination of highncolors
andn_prune
can be used to select for bright, sharp colors that are only in a small part of the image space.
- get the image in python (with a url or image path) using
cmp.get_image(url)
. Images are stored in thedata/
folder. - generate the colormap using
colors, cmap = cmp.get_colors_from_image(image, ncolors, n_prune)
. This returns both colors that can be used directly for lines etc. or a cmap object that can be used to color contours, 2d/3d plots and more. - visualise the image and colormap using
cmp.plt_image(image);cmp.plt_cmap(colors);
That's it!
There're a few examples in the demo Colab notebook, but feel free to try this out with your own images at the end of this tutorial (if you're running it on google colab). Play around with the parameters till you find something that works best for you! If you create any cool palettes using this, please attribute this package, and let me know!