Extracting the color palette of an image
Click here for the app!
If you want to contribute to the development of the code, visit the GitHub repository for the project.
The palette is the range of colors that someone (originally a painter) uses to do things (originally paintings). The palette defines greatly the visual style of any artist. In general, it defines the visual style of any colored communication; this could be data, a power point presentation, a journal cover, a report, a proposal for fundings, and so on.
I regularly read the news in r-bloggers, where I found this post of David Smith. There, he talks about using R palettes inspired in Wes Anderson movies. The idea is that although R has some interesting palettes, mass-produced palettes can be boring. It may be difficult for a user to create its own palette by picking colors one by one. A alternative approach is to extract the palette from a given image that the user may find interesting, beautiful, or inspiring. The code here does so with R.
Extracting the palette from an image is not as simple as it may appear: virtually every pixel in most images has a different position within the RGB color space. To reduce the number of colors from thousands to a few is not a trivial problem, although it could be done in many different ways. The challenge is to do so in a way that the "color style" is not lost in translation, at any palette resolution. This means that the palette must always make sense from the point of view of a human observer, and not (only) for a matrix-oriented machine. I have implemented a simple and more or less aesthetic algorithm to do so in this Shinny app. An earlier version of the code is included in the following R function (more advanced versions may be found in GitHub):
I regularly read the news in r-bloggers, where I found this post of David Smith. There, he talks about using R palettes inspired in Wes Anderson movies. The idea is that although R has some interesting palettes, mass-produced palettes can be boring. It may be difficult for a user to create its own palette by picking colors one by one. A alternative approach is to extract the palette from a given image that the user may find interesting, beautiful, or inspiring. The code here does so with R.
Extracting the palette from an image is not as simple as it may appear: virtually every pixel in most images has a different position within the RGB color space. To reduce the number of colors from thousands to a few is not a trivial problem, although it could be done in many different ways. The challenge is to do so in a way that the "color style" is not lost in translation, at any palette resolution. This means that the palette must always make sense from the point of view of a human observer, and not (only) for a matrix-oriented machine. I have implemented a simple and more or less aesthetic algorithm to do so in this Shinny app. An earlier version of the code is included in the following R function (more advanced versions may be found in GitHub):
extractpalette_function.r | |
File Size: | 4 kb |
File Type: | r |
The function reads an array of three matrixes (red, green and blue). There are many ways to get to that from your computer (for example, the readJPEG function in the jpeg package). I have not implemented that in the function because you may want to play a little with the picture before extracting its palette. For example, you may want to reduce its resolution.
The function has some options. The most important, from my point of view, is the number of chips that you want to include in your palette. This is the palette's resolution. For example, these are palettes of different resolutions for the self-portrait of Vincent van Gogh:
There are a number of options that you can discover by playing around with the code. The Shiny app is intended to be more user-friendly than the raw code. In any case, I hope you can find this useful.