r piping image_annotate doesn't work as expected

你说的曾经没有我的故事 提交于 2021-01-28 06:13:03

问题


I am trying to using magick to create an animated gif from a bunch of images. It works just fine but I wanted to annotate text (basically the file name) to each image before creating the gif - and that doesn't work.

I can't find the cause of the error (below) - not sure if it is the piping notation, the map function, or something else.

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

I get this error: Error in inherits(image, "magick-image") : argument "image" is missing, with no default


回答1:


It looks to me as though the error might be in nesting your map.

Since you have already mapped during image_read, it is not necessary to do so again for image_annotate,

Edit So we need to apply the function image_annotate to each element in the list returned by the mapped image_read. Try replacing map(image_annotate("Text") %>% with :

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

or

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

Reference for lapply()



来源:https://stackoverflow.com/questions/46921733/r-piping-image-annotate-doesnt-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!