I have been playing with package called imager, documentation on this package was extremely helpful! I have read through “getting started” as well as few other tutorial &amp; examples.
I love colours… Tools like colourlovers, Adobe Color CC, Canva Color Palette Generators are great extracting colours from photo (or in general just getting inspirations for colour palette), but I wanted to figure out the way to extract colours from image using R.
Preps
Loading up packages &amp; loading up image to R, and extracting RGB info out of image. First I need to load up image. I’m using below abstract image with bunch of different colours that I’ve created with Photoshop just for fun.
library(tidyverse) ##
library(imager) ## such a fun package. I want to learn more.
library(treemap) ## library(ggvoronoi) ## because I'm currently addicted to voronoi diagram.
## Load up the image using load.image function! im % head()
## x y red green blue hexvalue value
## 1 1 1 0.001528153 0.6012714 0.7346048 #0099BB 0.4360151
## 2 2 1 0.003336439 0.5943815 0.7305651 #0198BA 0.4320482
## 3 3 1 0.005112951 0.6265604 0.7657791 #01A0C3 0.4554402
## 4 4 1 0.005520480 0.6726386 0.8097380 #01ACCE 0.4875841
## 5 5 1 0.011926270 0.7021898 0.8367487 #03B3D5 0.5099122
## 6 6 1 0.011128983 0.6695389 0.8149510 #03ABD0 0.4880113
Applying K-Means Algorithm on RGB value from Image
Using kmeans function is pretty simple, I’m selecting 12 as k in below example, simply because I wanted to get 12 distinct colours from the picture.
## Pick k value to run kMean althorithm.
## But to extract colours, I'd pick k as number I want back! my_k %
inner_join(im_df %__% count(cluster_num))
## I can also save the colour palette for future use as well.
my_colour % ggplot(aes(x=(cluster_num-1)%%num_row,y=floor((cluster_num-1)/num_row))) + geom_tile(aes(fill=group_hex)) +
geom_label(aes(label=paste(cluster_num,&quot;:&quot;,group_hex,&quot;\n&quot;,n,&quot;distinct hexvalues&quot;)), family=&quot;Roboto Condensed&quot;, lineheight=0.8) +
scale_fill_manual(values=sort(kmean_center$group_hex), guide=&quot;none&quot;) +
theme_void(base_family=&quot;Roboto Condensed&quot;) +
labs(subtitle=paste0(&quot;k-Mean clustering center colour with &quot;, my_k, &quot; clusters&quot; )) +
scale_y_reverse()
Which colours get clustered together?
I wanted to see which colours were bundled together in same cluster. I took a sample because plotting more colours simply just took way too long on my machine…
im_df %__% sample_n(size=10000) %__% ## I'm just going to take sample to make the drawing bit faster...
count(cluster_num,hexvalue) %__%
treemap(index=c(&quot;cluster_num&quot;,&quot;hexvalue&quot;),
type=&quot;color&quot;,
vSize=&quot;n&quot;, vColor=&quot;hexvalue&quot;,
algorithm = &quot;squarified&quot;,
fontfamily.labels=&quot;Roboto Condensed&quot;, fontfamily.title=&quot;Roboto Condensed&quot;,
border.col=c(&quot;#ffffff&quot;,&quot;#ffffff50&quot;),
fontsize.labels=c(24,0),
aspRatio=16/9,
title=&quot;Clustering with RGB&quot;)
Bonus: Voronoi Abstract Art
Below is more fun to do on other images, but I wanted to just use ggvoronoi package because I’m currently in love…
Looks like stained glass! :)
## Create Mini sets, choose to select grayscale value that are bigger more often.
im_df_mini %
sample_n(size=1000, weight=(1-value)) im_df_mini %__%
ggplot(aes(x=x,y=y)) +
geom_voronoi(aes(fill=hexvalue), color=&quot;#000000&quot;, size=0.1) +
scale_fill_manual(values=sort(unique(im_df_mini$hexvalue)), guide=&quot;none&quot;) +
theme_void() +
scale_y_reverse() +
coord_fixed()
Using Plotly package
I’ve always been curious to use plotly, but I haven’t had chance to yet. Since RGB values are 3 different values, I wanted to use xyz axis to plot (and I didn’t know how to do that in ggplot2), so here’s yet another way to view how the colour got clustered more visually.
library(plotly)
im_df %__% sample_n(size=2000) %__% plot_ly(x = ~red, y=~blue, z=~green, color=~cluster_num, colors=my_colour, size=~value) %__%
add_markers()