README.md

ditherer

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.

Installation

remotes::install_github('cj-holmes/ditherer)

Add package to search path

library(ditherer)

# ggplot2 for convenience in readme
library(ggplot2)

Typical workflow

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")
#> Warning in if (target_palette == "c216") {: the condition has length > 1 and
#> only the first element will be used
#> Warning in if (target_palette == "c64") {: the condition has length > 1 and only
#> the first element will be used

Default 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")

Greyscale target palette

dither(img, target_palette = grey.colors(8))
#> Warning in if (target_palette == "c216") {: the condition has length > 1 and
#> only the first element will be used
#> Warning in if (target_palette == "c64") {: the condition has length > 1 and only
#> the first element will be used

Other examples

Splash

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")

Mandrill (a.k.a. Baboon)

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")



cj-holmes/ditherer documentation built on Feb. 25, 2021, 2:59 p.m.