r 管道 image_annotate 没有按预期工作

r piping image_annotate doesn't work as expected

我正在尝试使用 magick 从一堆图像中创建动画 gif。它工作得很好,但我想在创建 gif 之前为每个图像注释文本(基本上是文件名)——但这是行不通的。

我找不到错误的原因(如下)- 不确定是管道表示法、映射函数还是其他原因。

library(purrr)
library(magick)

#set working directory with a couple of png's
#This works:
image_read("image1.png") %>% image_annotate("Text")    

#and this works too:
list.files(path = "", pattern = "*.png", full.names = T) %>% 
      map(image_read) %>%
      image_join() %>% 
      image_animate(fps=1) %>% 
      image_write("animated.gif")

#but this doesn't:
list.files(path = "", pattern = "*.png", full.names = T) %>% 
      map(image_read) %>%
      map(image_annotate("Text")) %>%
      image_join() %>% 
      image_animate(fps=1) %>% 
      image_write("animated.gif")

我收到这个错误: Error in inherits(image, "magick-image") : argument "image" is missing, with no default

在我看来,错误可能出在嵌套您的地图上。

由于您已经在 image_read 期间进行了映射,因此无需在 image_annotate

期间再次映射

编辑 所以我们需要将函数 image_annotate 应用于映射 image_read 返回的列表中的每个元素。尝试将 map(image_annotate("Text") %>% 替换为 :

lapply(image_annotate("Text")) %>%

lapply(. %>% image_annotate("Text")) %>%

Reference for lapply()