Package: pylette
PyletteA color palette extractor written in Python using KMeans clustering.MotivationWorking with computer graphics and visualizations, one often needs a way of specifying a set of colors
with a certain visual appeal. Such a set of colors is often called acolor palette. The aim of this
library is to easily extract a set of colors from a supplied image, with support for the various color modes (RGB, RGBa, HSV, etc).
Dabbling in generative art, the need often arises for being able to pick colors at random from a palette.
Pylette supports this, both picking colors uniformly, but also using the color frequency from the original image as probabilities.Other color palette related Python-libraries:Color Thief: Extraction of color palettes using the median cut algorithm.Palettable: Generation of matplotlib compatible color schemesColorgram: Extraction of colors from images (similar to the intended use of this library),
however, I was unable to install this.InstallationPylette is available in the python package index (PyPi), and can be installed usingpip:pipinstallPyletteBasic usageAPaletteobject is created by calling theextract_colorsfunction, either using a path to an image, or an image url:fromPyletteimportextract_colorspalette=extract_colors(image='image.jpg',palette_size=10,resize=True)palette=extract_colors(image_url='https://path.to.image',palette_size=10,resize=True,mode='MC',sort_mode='luminance')This yields a palette of ten colors, and theresizeflag tells Pylette to resize the image to a more manageable size before
beginning color extraction. This significantly speeds up the extraction, but reduces the faithfulness of the color palette.
One can choose between color quantization using K-Means (default) or Median-Cut algorithms, by setting in themode-parameter. One can also specify to alternatively sort the color palette by the luminance (percieved brightness).The palette object supports indexing and iteration, and the colors are sorted from highest to lowest frequency by default.
E.g, the following snippet will fetch the most common, and least common
color in the picture if the palette was sorted by frequency, or the darkest to lightest color if sorted by luminance:most_common_color=palette[0]least_common_color=palette[-1]three_most_common_colors=palette[:3]As seen, slicing is also supported.The Palette object contains a list of Color objects, which contains a representation of the color in various color modes, with RGB being the default. Accessing the color attributes is easy:color=palette[0]print(color.rgb)print(color.hls)print(color.hsv)To display the extracted color palette, simply call thedisplay-method, which optionally takes a flag for saving the palette to an image file.
The palette can be dumped to a CSV-file as well, where each row represents the RGB values and the corresponding color frequency (optional).palette.display(save_to_file=False)palette.to_csv(filename='color_palette.csv',frequency=True)In order to pick colors from the palette at random, Pylette offers therandom_color-method, which supports both drawing
uniformly, and from the original color distribution, given by the frequencies of the extracted colors:random_color=palette.random_color(N=1,mode='uniform')random_colors=palette.random_color(N=100,mode='frequency')Example PalettesA selection of example palettes. Each palette is sorted by luminance (percieved brightness). The top row corresponds to extraction using K-Means, and the bottom row corresponds to Median-Cut extraction.Original ImageExtracted PaletteCommand Line ToolThe new version of Pylette also comes bundled with a command line tool, which can be used to extract color palettes from the command line.usage:pylette[-h](--filenameFILENAME|--image-urlIMAGE_URL)[--mode{KM,MC}][--nN][--sort_by{luminance,frequency}][--stdoutSTDOUT][--colorspace{rgb,hsv,hls}][--out_filenameOUT_FILENAME][--display-colorsDISPLAY_COLORS]options:-h,--helpshowthishelpmessageandexit--filenameFILENAMEpathtoimagefile(default:None)--image-urlIMAGE_URLurltotheimagefile(default:None)--mode{KM,MC}extraction_mode(KMeans/MedianCut(default:KM)--nNthenumberofcolorstoextract(default:5)--sort_by{luminance,frequency}sortbyluminanceorfrequency(default:luminance)--stdoutSTDOUTwhethertodisplaytheextractedcolorvaluesinthestdout(default:True)--colorspace{rgb,hsv,hls}colorspacetorepresentcolorsin(default:RGB)--out_filenameOUT_FILENAMEwheretosavethecsvfile(default:None)--display-colorsDISPLAY_COLORSOpenawindowdisplayingtheextractedpalette(default:False)Under the hoodCurrently, Pylette uses KMeans for the color quantization. There are plans for implementing other color quantization schemes, like:Median-cut [Implemented]OctreeModified minmaxThe articleImproving the Performance of K-Means for Color Quantizationgives a
nice overview of available methods.FeedbackAny feedback and suggestions is much appreciated.
This is very much a work in progress.