1. Backgound


2. Set-up

Load all required packages for analysis.

library(tidyverse)    #for data manipulation
library(jpeg)
library(scales)
library(usethis)
library(knitr)


3. Import images

Import all images.

file.list <- list.files(pattern='*.jpg')
images <- suppressMessages(map(file.list, ~ readJPEG(.)))
names(images) <- gsub("\\.jpg", "", file.list)

list2env(images,envir=.GlobalEnv)


4. Use K-means clustering to extract palettes from images.

Reference: http://www.milanor.net/blog/build-color-palette-from-image-with-paletter/

set.seed(42)

extract_pal <- function(image){
dimension    <- dim(image)
image_rgb <- data.frame(
  x = rep(1:dimension[2], each = dimension[1]),
  y = rep(dimension[1]:1, dimension[2]),
  R = as.vector(image[,,1]), #slicing our array into three
  G = as.vector(image[,,2]),
  B = as.vector(image[,,3])
)

k_means <- kmeans(image_rgb[,c("R","G","B")], centers = 20, iter.max = 30)

}

list <- list(aberdeen=aberdeen, 
             cornwall=cornwall, 
             harrogate=harrogate, 
             penzance=penzance,
             somerset=somerset, 
             wales=wales, 
             yorkshirecoast=yorkshirecoast)

out <- map(list, ~extract_pal(.))

railpalettes <- map(out, ~ as_tibble(pluck(.,"centers")))

map(railpalettes, ~ show_col(rgb(.)))

railpalettes <- map(railpalettes, ~ rgb(.))

keepcols <- c("#7A5532", "#729BB2", "#BA1A14", "#A69D91", "#A09E30", "#554D54",
              "#F7E5BB", "#180D0D", "#B0B4B7", "#195C91", "#E9C27E", "#E1A93B",

             "#674A49", "#3C2B4E", "#BEA899", "#CB9A6A", "#140D31",
             "#756E63", "#5B2731", "#D7CB76", "#AF7B57", "#9DBFB9", "#935945",
             "#8AA5A9", "#8B8F6A",

             "#AC362B", "#A5936F", "#37302F", "#C6715C", "#565958", "#683F28",
             "#827F77", "#080C1D", "#04050D", "#0F2138",

             "#D7B554", "#4ECDDA", "#13513F", "9BB21A", "#4D9890", "#1572C3",
             "#9E857A", "#F26B07", "#8C181E", "#401341",

             "#BCBEBC", "#E1DECB", "#E19B3F", "#3B4864", "#1D1E22", "#B56A35",
             "#54443D", "#CCAA44", "#6D7473",

             "#706973", "#4240C0", "#1C2421", "#252D41", "#AAB9D6", "#C2CAA2",
             "#989B86", "#35375D",

             "#837032", "#5C655C", "#486791", "#1F2C22", "#3C4142", "#EBD48E",
             "#8DA6B1", "#7A837E"

              )

railpalettes <- map(railpalettes, ~ purrr::keep(.x, ~ .x %in% keepcols))

railpalettes




#Remove some very light colours (not useful for plotting)

use_data(railpalettes, overwrite = TRUE)


5. Reproducibility

This reproduction of the analysis was run by:

sysinfo <- Sys.info()

sysinfo <- data.frame(keyName=names(sysinfo), value=sysinfo, row.names=NULL)

sysinfo %>% kable()

Analysis was run at r Sys.time(), and using the following Session Info:

sessionInfo()


petermacp/pmthemes documentation built on Aug. 16, 2020, 11:19 p.m.