Skip to content

Instantly share code, notes, and snippets.

@chichacha
Last active January 26, 2019 21:34
Show Gist options
  • Save chichacha/f7f292e850473bbe1e6dab1c19cde4be to your computer and use it in GitHub Desktop.
Save chichacha/f7f292e850473bbe1e6dab1c19cde4be to your computer and use it in GitHub Desktop.
Abstraction of Text -
library(tidyverse)
library(magick)
library(imager)
library(ggvoronoi)
library(TSP)
create_char_image <- function(char="美"){
data.frame() %>%
ggplot(aes(x=0,y=0)) +
geom_text(label=char, family="Osaka", size=120) +
theme_void() +
coord_fixed()
ggsave(file="hiragana_a.png", width=9, height=9)
a <- image_read("hiragana_a.png") %>% image_resize("600x")
df <-a %>% image_canny() %>%
magick2cimg() %>%
as.data.frame() %>%
filter(value==1)
df_mini <- df %>% sample_n(nrow(df)*0.5)
tsp <- df_mini %>% select(x,y) %>% dist() %>% TSP()
#glimpse(tsp)
tour <- solve_TSP(tsp, method="nn")
df_tsp <-df_mini[tour,] %>%
mutate(idx = row_number())
return(df_tsp)
}
df_tsp <-create_char_image("猪")
draw_character <- function(m=25){
base <- df_tsp %>% ggplot(aes(x=x,y=y, color=idx, group=idx%%m)) +
#stat_voronoi(geom="path", size=0.1, data=.%>%sample_n(nrow(.)*0.2)) +
scale_y_reverse() +
coord_fixed() +
theme_void() +
scale_color_viridis_c(guide="none") +
scale_fill_viridis_c(alpha=0.2)
base + geom_path(size=0.1)
}
fig <- image_graph(width=800, height=600)
c(3,5,25,45,90,250,500,300,200,100,50,30,25,5) %>% map(., draw_character)
dev.off()
image_animate(fig, fps=5) %>% image_write("pig_abstract_text.gif")
@chichacha
Copy link
Author

pig_abstract_text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment