Textify

        

Right in the section of R fun and totally unnessary, this blog post from Giora Simchoni showed how R could be used to create a “textifed” image.

nationalAnthem.ch.en <- 
  "When the morning skies grow red
And o'er their radiance shed,
Thou, O Lord, appeareth in their light.
When the Alps glow bright with splendour,
Pray, free Swiss, Pray,
For you feel and understand,
For you feel and understand,
That he dwelleth in this land.
That he dwelleth in this land.
In the sunset Thou art nigh
And beyond the starry sky,
Thou, O loving Father, ever near
When to Heaven we are departing,
Joy and bliss Thou'lt be imparting,
For we feel and understand
For we feel and understand
That Thou dwellest in this land.
That Thou dwellest in this land.

When dark clouds enshroud the hills
And gray mist the valley fills,
Yet Thou art not hidden from Thy sons.
Pierce the gloom in which we cower
With Thy sunshine's cleansing power
Then we'll feel and understand
Then we'll feel and understand
That God dwelleth in this land.
That God dwelleth in this land.
Towards us in the wild storm coming,
You yourself give us resistance and stronghold,
You, almighty ruling, rescuing!
During horror and nights of thunderstorms
Let us childlike trust Him!
Yes, we feel and understand;
Yes, we feel and understand;
That God dwelleth in this land.
That God dwelleth in this land."

image.url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Flag-map_of_Switzerland.svg/640px-Flag-map_of_Switzerland.svg.png"

# make a matrix of the image
img <- load.image(image.url) %>% resize(700, 500)
plot(img)

imgMat <- img %>% 
  as.array() %>% adrop(3) %>% aperm(c(2, 1, 3))


text <- paste(nationalAnthem.ch.en, collapse = " ")
text <- str_replace_all(text, "\n+", " ")
text <- str_replace_all(text, " +", " ")
text <- str_split(text, "")[[1]]

strwidth
## function (s, units = "user", cex = NULL, font = NULL, vfont = NULL, 
##     ...) 
## {
##     if (!is.null(vfont)) 
##         vfont <- c(typeface = pmatch(vfont[1L], Hershey$typeface), 
##             fontindex = pmatch(vfont[2L], Hershey$fontindex))
##     .External.graphics(C_strWidth, as.graphicsAnnot(s), pmatch(units, 
##         c("user", "figure", "inches")), cex, font, vfont, ...)
## }
## <bytecode: 0x7f96d7b74a28>
## <environment: namespace:graphics>

comments powered by Disqus