Created
April 25, 2019 13:17
-
-
Save brodieG/b2e072a7df12437bff568f5232da3131 to your computer and use it in GitHub Desktop.
Convert PNG to HTML in Rmarkdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PNG to HTML | |
Code adapted from [@gdagstn](https://gist.github.com/gdagstn/1917e0e6f7dc91a8dde7cb789326c3bb). | |
```{r, echo=FALSE, comment="", results="asis"} | |
old.hooks <- fansi::set_knit_hooks(knitr::knit_hooks) | |
``` | |
```{r echo=FALSE} | |
library(crayon) | |
library(readbitmap) | |
options(crayon.enabled=TRUE) | |
# example file | |
# download.file('http://pixeljoint.com/files/icons/full/32x32gameboypaletteportrait16.01.06.png', destfile = "./logo.png", method = "wget", extra = "-r -p --random-wait") | |
file <- "~/Downloads/32x32gameboypaletteportrait16.01.06.png" | |
setClass("crayonmap", representation(crayons = "character", row.num = "numeric", col.num = "numeric")) | |
#' Create crayonmap object | |
#' @param file BMP, TIFF, JPG or PNG bitmap file | |
#' @return a "crayonmap" class object to be used by paintCrayon() | |
readCrayonMap <- function(file) | |
{ | |
require(crayon) | |
require(readbitmap) | |
pic <- read.bitmap(file) | |
rgbmat <- rgb(matrix(pic[1:nrow(pic), 1:ncol(pic),], nrow = nrow(pic) * ncol(pic))) | |
crayonstrings = matrix(sapply(rgbmat, function(x) crayon::make_style(x, bg = T)(" ")), nrow = nrow(pic)) | |
crayonstrings[,ncol(crayonstrings)] <- paste(crayonstrings[,ncol(crayonstrings)], "\n", sep = "") | |
crayonstrings[,1] <- paste(" ", crayonstrings[,1], sep = "") | |
crayons <- as.vector(t(crayonstrings)) | |
cmap <- new("crayonmap", crayons = crayons, row.num = nrow(pic), col.num = ncol(pic)) | |
return(cmap) | |
} | |
#' Text-based bitmap | |
#' @param cmap object of class "crayonmap" | |
#' @return a text-based bitmap directly in the terminal output. | |
paintCrayon <- function(cmap) | |
{ | |
cat("\n \n \n") | |
cat(cmap@crayons, sep = "") | |
cat("\n \n \n") | |
} | |
cmap <- readCrayonMap(file) | |
paintCrayon(cmap) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment