问题描述
我想用每个文件的名称替换image_annotate下面的文本".
I would like to replace the "Text" below in image_annotate by the name of each file.
library(dplyr)
library(purrr)
library(magick)
list.files(path = "", pattern = "*.png", full.names = T) %>%
map(image_read) %>%
lapply(. %>% image_annotate("Text")) %>%
image_join() %>%
image_animate(fps=1) %>%
image_write("animated.gif")
推荐答案
我认为这应该可行.由于您获得了有关文件名的信息并将其传递给image_read
,但又希望将此信息也用于另一个函数(image_annotate
),因此可以轻松地将两者放在map
中的函数中.如果您不希望将整个路径用作注解,只需将image_annotate(fnme)
替换为image_annotate(stringr::str_extract(string = fnme, pattern = "(?<=(/|\\\\)).+(?=\\.png)"))
,即可匹配/
或\
与.png
之间的任何内容(假设您不希望结尾,否则只需删除前瞻(?=\\.png
).
I think that this should work. Since you get the information about the filename and pass it to the image_read
but want to use this information also for another function (image_annotate
), you can easily put the two in a function within map
. If you don't want the whole path as annotation, just replace image_annotate(fnme)
by image_annotate(stringr::str_extract(string = fnme, pattern = "(?<=(/|\\\\)).+(?=\\.png)"))
which matches anything between /
or \
and .png
(assuming that you don't want the ending, else just remove the lookahead (?=\\.png
).
library(dplyr)
library(purrr)
library(magick)
list.files(path = "", pattern = "*.png", full.names = T) %>%
map(function(fnme) {
image_read(fnme) %>% image_annotate(fnme)
}) %>%
image_join() %>%
image_animate(fps=1) %>%
image_write("animated.gif")
这篇关于r dplyr-读取文件列表并将文件名用作变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!