knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(ransid)
library(magick)

ransid

Lifecycle: experimental

ransid converts images to ANSI text which can be displayed in the console.

Installation

You can install ransid from github with:

# install.packages('remotes')
remotes::install_github("coolbutuseless/ransid")

Example - 216 colour ANSI

Note - because ANSI is for display in a terminal, it does not render in a markdown document. The following images are screenshots of my Rstudio terminal

im <- magick::image_read(system.file('img', 'Rlogo.png', package = 'png'))
cat(im2ansi(im, width = 120))
knitr::include_graphics("man/figures/R.png")
im <- magick::image_read('https://www.fillmurray.com/300/250')
cat(im2ansi(im, width = 120))
knitr::include_graphics("man/figures/murray.png")
im <- magick::image_read('https://placekitten.com/300/200')
cat(im2ansi(im, width = 120))
knitr::include_graphics("man/figures/kitten.png")

Example - 24bit ANSI colour

24bit colour ANSI is enabled by setting full_colour = TRUE

24bit ANSI colour has more limited support (compared to 8bit ANSI), and will not display in Rstudio.

It will however work in iTerm on OSX, KDE Konsole, Xterm, libvte-based terminals such as GNOME Terminal. See the wikipedia page for more information

im <- magick::image_read('https://placekitten.com/300/200')
cat(im2ansi(im, width = 200, full_colour = TRUE))
knitr::include_graphics("man/figures/kitten24.png")

Access raw ANSI

Lower level functions are also available.

col2bg

col2bg('tomato')
paste0("Hello ", col2bg("tomato"), "#RStats", reset_code)
cat(paste0("Hello ", col2bg("tomato"), "#RStats", reset_code), "\n")
knitr::include_graphics("man/figures/text1.png")

col2fg

col2fg('darkgreen')
paste0("Hello ", col2bg('tomato'), col2fg('darkgreen'), "#RStats", reset_code)
cat(paste0("Hello ", col2bg('tomato'), col2fg('darkgreen'), "#RStats", reset_code), "\n")
knitr::include_graphics("man/figures/text2.png")

im2char

im <- image_read(system.file('img', 'Rlogo.png', package = 'png'))
char_matrix <- im2char(im)
char_matrix[1, 1:10]

col2bg24

col2bg24('tomato')
paste0("Hello ", col2bg24("tomato"), "#RStats", reset_code)
cat(paste0("Hello ", col2bg24('tomato'), col2fg24('darkgreen'), "#RStats", reset_code), "\n")
knitr::include_graphics("man/figures/text3.png")

Related Software

Some great R packages for ANSI handling that eclipse what this humble package does:



coolbutuseless/ransid documentation built on Oct. 16, 2020, 5:06 p.m.