pick_palette_jk: Pick color palette from a picture

Description Usage Arguments Examples

View source: R/pick_palette_jk.R

Description

This function uses clustering on a subset of pixels from given picture. The final output is mean of pixels in each cluster converted to hex. Agnes is computationaly much more intensive than kmeans. Hierarchical clustering is primarily used to construct the whole dendrogram. So we can just ouput the whole tree and let you plot or get colors afterwards. Use function tree2color from this package. However if you choose the kmeans algorithm, you can only get one vector of colors.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pick_palette_jk(
  path = NULL,
  k = 3,
  plot_image = F,
  algorithm = "agnes",
  grid = 60,
  full_object = T,
  linkage = "single",
  kmax = 10
)

Arguments

path

Path used to get the .jpg picture. Before running this function you can run file.choose() function to get the desired path.

k

Desired number of colors in a palette.

plot_image

Should the original image be plotted. The default value is FALSE.

algorithm

Two methods are implemented. The currently available are "agnes", "diana" and "kmeans". The default is "agnes".

grid

The size of grid used for pixel sampling.

linkage

Link used in hierarchical clustering. Default is "single".

kmax

Maximal k wanted in the tree while using diana.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
path <- "./man/figures/duck.jpg"
tree <- pick_palette_jk(path) #keep in mind this can take up to 5 mins
colours <- tree2color(tree, k = 10)
plot_colours(colours)

## using diana
tree <- pick_palette_jk(path,algorithm = "diana")
tree2color(tree, k = 10) %>% sort_colours %>% plot_colours

## or use something like this
color_list <- purrr::map(1:10,~tree2color(tree,.x))
# do.call(pals::pal.bands,color_list) # commented out in cas pals fails to install
#compare this with
plot_colours(rgb(pick_palette_jk(path,algorithm = "kmeans",k = 10)))
#hierarchical clustering has great potential but is really slow compared to kmeans

jakubkovac/Lori documentation built on Jan. 9, 2020, 9:49 p.m.