Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an UTF8 encoded emoji character to an image?

Tags:

regex

r

utf-8

emoji

I have a long vector containing emojis, something like this:

emojis <- c("😐","😥","😴","😉","😛")

and then I have their utf-8 encodings which I extracted with the help of devtools::install_github( "ThinkRstat/utf8splain") followed by library(utf8splain) like this, just for example: (not accurately represented)

emojis_enc <- c()
for(i in 1:length(emojis)){
  emojis_enc <- c(emojis_enc, utf8splain::runes(emojis[i])$rune)
}
#emojis_enc <- c("U+12345","U+67891","U+91234","U+56789","U+123A6")

Then I have made a function this:

emojiPlot <- function(photo_enc, emo_char){
  png(paste0(photo_enc, ".png"))
  plot(emo_char, rescale = T, ylim = c(-1,1), xlim = c(-1,1))
  dev.off()
}

And when I do this emojiPlot("emojis_enc[1]", emojis[1]) I just get an empty plot with x and y axis. I am pretty novice with UTF and R.

The reason why I am doing is because I am using a plugin called "imagepreview" in gephi software. And the plugin requires the nodes as photos. So my goal is to have individual emojis in .png and give them their utf encodings as the name. So when I import my data, I can just point the CSV containing the utfs to the names of the photos and pull the right emoji. And then just graph it.

I am doing this for a research project. I am open to better ways to do it.

I am in Ubuntu OS. If you use Windows, then emojis will show as their correct utf format, and won't be rendered.

like image 260
CaseebRamos Avatar asked Nov 29 '25 22:11

CaseebRamos


1 Answers

Another solution to get to the png files of the emojis is to download them from Twitter, which accepts runes in the urls linking to the png files:

library(tidyverse)
data <- tibble(emojis = c("😐","😥","😴","😉","😛")) %>% 
  mutate(rune = map_chr(emojis, ~ utf8splain::runes(.)$rune)) %>%    # convert to runes
  mutate(rune = str_remove(rune, fixed("U+"))) %>%                   # remove leading U+
  mutate(emoji_url = paste0("https://abs.twimg.com/emoji/v2/72x72/", # make url
                            tolower(rune), ".png"))

# download the files
map2(data$emoji_url, paste0(data$rune, ".png"), function(x, y) download.file(x, y, method = "curl"))

This will download the png files and place them in your working directory.

In base

The tidyverse code is optional, the same can be done in base:

emojis <- c("😐","😥","😴","😉","😛")
rune <- sapply(emojis, function(x) utf8splain::runes(x)$rune)
emojiurl <- paste0("https://abs.twimg.com/emoji/v2/72x72/", tolower(rune), ".png")

for (i in seq_along(emojiurl)) {
  download.file(emojiurl[i], paste0(rune[i], ".png"), method = "curl")
}
like image 131
JBGruber Avatar answered Dec 02 '25 13:12

JBGruber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!