如何将 UTF8 编码的表情符号字符转换为图像?

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

我有一个包含表情符号的长向量,如下所示:

emojis <- c("","","","","")

然后我得到了他们的 utf-8 编码,我在 devtools::install_github( "ThinkRstat/utf8splain") 的帮助下提取了它们,然后是 library(utf8splain) 就像这样,例如:(未准确表示)

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")

然后我做了一个函数:

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()
}

当我这样做时 emojiPlot("emojis_enc[1]", emojis[1]) 我只得到一个带有 x 轴和 y 轴的空图。我是 UTF 和 R 的新手。

我之所以这样做是因为我在gephi软件中使用了一个名为“imagepreview”的插件。并且该插件需要节点作为照片。所以我的目标是在 .png 中有单独的表情符号,并将它们的 utf 编码作为名称。因此,当我导入数据时,我只需将包含 utfs 的 CSV 指向照片的名称,然后拉出正确的表情符号。然后绘制它。

我正在为一个研究项目做这件事。我愿意接受更好的方法。

我在UbuntuOS。如果您使用 Windows,则表情符号将显示为正确的 utf 格式,并且不会被渲染。

当您应该使用 text.
时,您正在使用 plot 绘制字符 在下面的代码中,使用 plot、参数 type = "n" 创建绘图,意思是 不要绘图 。然后添加文本(表情符号)。

emojiPlot <- function(photo_enc, emo_char){
  png(paste0(photo_enc, ".png"), units = "cm",
      width = 10, height = 10, res = 600)
  plot(0, 0, xlim = c(-1, 1), ylim = c(-1, 1), type = "n")
  text(0, 0, emo_char)
  dev.off()
}

emojiPlot("emojis_enc[1]", emojis[1])

文件 "emojis_enc[1].png" 发布在这里:

获取表情符号的 png 文件的另一种解决方案是从 Twitter 下载它们,它接受链接到 png 文件的 url 中的符文:

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"))

这将下载 png 文件并将它们放在您的工作目录中。

在基地

tidyverse代码是可选的,同样可以在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")
}