Search code examples
rimagemagickanimated-gifpurrrpiping

r piping image_annotate doesn't work as expected


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


Solution

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