knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", dpi = 300, out.width="45%", fig.width=4, fig.height=4 )
This package makes an attempt at implementing full colour ordered (Bayer matrix) dithering in R. The engine behind the image handling is the magick
package.
remotes::install_github('cj-holmes/ditherer)
Add package to search path
library(ditherer) # ggplot2 for convenience in readme library(ggplot2)
Path to original image
img <- 'data-raw/lenna.png'
Create a target palette for the original image. Here I do that using the colorfindr
package.
# Create a 16 colour target palette from the image set.seed(1) tp <- colorfindr::get_colors(img) %>% colorfindr::make_palette(n = 16, show = FALSE) # View the target palette data.frame(x = tp) %>% ggplot(aes(x="", fill = x))+ geom_bar()+ coord_flip()+ scale_fill_identity()+ theme_void()
Recreate the original image using only the target palette
# Original dither(img, original = TRUE) + labs(caption="Original") # Dithered (target palette) dither(img, target_palette = tp) + labs(caption = "16 colour target palette")
By default, if a target palette is not supplied, ditherer
uses a uniform palette made of 216 colours.
View the default palette
data.frame(x = ditherer::c216) %>% ggplot(aes(x="", fill = x))+ geom_bar()+ coord_flip()+ scale_fill_identity()+ theme_void()
Varying the target palette colour spread r
factor
dither(img, r=1/8) + labs(caption = "r = 1/8 (default)") dither(img, r=1/6) + labs(caption = "r = 1/6") dither(img, r=1/4) + labs(caption = "r = 1/4")
dither(img, target_palette = grey.colors(8))
dither('http://sipi.usc.edu/database/preview/misc/4.2.01.png', original = TRUE) + labs(caption = "Original") dither('http://sipi.usc.edu/database/preview/misc/4.2.01.png') + labs(caption = "Dithering - default parameters and target palette")
dither('http://sipi.usc.edu/database/preview/misc/4.2.03.png', original = TRUE) + labs(caption = "Original") dither('http://sipi.usc.edu/database/preview/misc/4.2.03.png') + labs(caption = "Dithering - default target palette")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.