knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library(ransid) library(magick)
ransid
converts images to ANSI text which can be displayed in the console.
ransid
will map them to the nearest available ANSI colourYou can install ransid
from github with:
# install.packages('remotes') remotes::install_github("coolbutuseless/ransid")
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")
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")
Lower level functions are also available.
col2bg
, col2bg24
- create an ANSI sequence for a particular background colour (8bit and 24bit versions)col2fg
, col2fg24
- create an ANSI sequence for a particular foreground colour (8bit and 24bit versions)im2char
- the matrix corresponding to the image, where each element represents
one pixelcol2bg
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")
Some great R packages for ANSI handling that eclipse what this humble package does:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.