Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created February 7, 2021 05:00
Show Gist options
  • Save chichacha/c6271b8c1e01b632919837abcc0c97ed to your computer and use it in GitHub Desktop.
Save chichacha/c6271b8c1e01b632919837abcc0c97ed to your computer and use it in GitHub Desktop.
ASCII Art with Imager for Fun
library(tidyverse)
library(imager)
## Following http://dahtah.github.io/imager/ascii_art.html
img_url <-"https://live.staticflickr.com/65535/50916479098_dc9f441e76_c.jpg"
im_orig <- load.image(img_url)
im_orig %>% plot()
## How big is my Data Frame going to be in terms of number of rows...
dim(im_orig)[1]*dim(im_orig)[2] ## since my image is 800x800 px...If I convert image to data frame, it will be pretty big dataframe...
## I want bit smaller image so that data frame is not going to be HUGE!
desired_x <- 100
scale_factor <- desired_x/((dim(im_orig)[1]))
im <- im_orig %>% imresize(scale=scale_factor)
#dim(im)[1]*dim(im)[2]
## Test to make sure it's still visible
im %>% plot()
## Function to Convert Character Image to Average of Value
chr_darkness <- function(chr) {
implot(imfill(50,50,val=1),text(25,25,chr,cex=5)) %>%
grayscale() %>% mean()
}
## Type in the text you want to use in illustration below.
chrs_to_use <- str_split("RStat*&%$#!",pattern="") %>% unlist() %>% unique()
## Create Alphabet Data Frame
alphabet_df <- tibble(
chr=chrs_to_use
) %>% mutate(darkness=map_dbl(chr,chr_darkness)) %>%
mutate(d_rank = row_number(darkness))
## Just to view what letter will be used
## You see letter with more ink on left and less ink on top right
alphabet_df %>%
ggplot(aes(x=d_rank,y=darkness)) +
geom_text(aes(label=chr)) +
theme_minimal() +
labs(title="Darkest Alphabet to Lightest Alphabet in Value")
## convert cimg to dataframe with rgb value then using rgb value conver to hex
im_df_color <- im %>% as.data.frame(wide="c") %>%
mutate(hex=pmap_chr(list(red=c.1,green=c.2,blue=c.3),rgb))
## create gray scale version of data frame too
im_df_gray <- im %>% grayscale() %>% as.data.frame()
## For this particular image, I'm just going to remove any "white part".
im_df_comb <- bind_cols(im_df_color, im_df_gray %>% select(value)) %>%
filter(hex!="#FFFFFF")
## Join with Alphabet Data Frame to assign characters for value
im_df_comb <- im_df_comb %>%
mutate(qv = scales::rescale(value,to=c(1,max(alphabet_df$d_rank))) %>% round()) %>%
inner_join(alphabet_df %>% select(chr, qv=d_rank))
im_df_comb %>% ggplot(aes(x=x,y=y)) +
geom_text(aes(label=chr, alpha=value, color=hex, size = value, family="Helvetica"),
data = . %>% filter(chr!="*")) +
scale_y_reverse() +
scale_color_identity() +
coord_fixed() +
theme_void() +
scale_alpha_continuous(range=c(0.7,1), guide="none")+
scale_size_continuous(range=c(5,10), guide="none")
## You can save the art
ggsave("Downloads/Chicken.svg", width=16, height=16)
ggsave("Downloads/Chicken.png", width=16, height=16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment