vectorize image_annotate() on series of gifs

I'm attempting to create a series of gifs with different overlay text. Later I will merge the gifs together to create an animation where the overlay text changes at different intervals. I have tried to vectorize my for loop unsuccessfully using lapply and map. I think the issue may be that image_annotate() only accepts a vectorized input for the text parameter and not the image parameter. Does anyone have suggestions of how I might be able to successfully vectorize the for loop? TIA.

library(tidyverse) 
library(magick) 

elm <- matrix(nrow=26, ncol=7) 
niagara <- rep(list(elm),250) 
images <- seq(1,250) 
overlay <- seq(2,500,2) 

falls_gif <- image_read('https://media.giphy.com/media/jpxjacGJcdies/giphy.gif') 
                                  
for (i in images){
  niagara[[i]] <- falls_gif
  niagara[[i]] <- niagara[[i]] %>% 
    image_annotate(paste0("Acre-Feet: ", overlay[i]))
}

Is this what you were trying to achieve?

library(tidyverse)
library(magick)

images = 1:250 %>% 
  map(~image_annotate(falls_gif, paste0("Acre-Feet: ", 2*.x), 
                      size=20, color="white"))
images[[5]]

preview

Combining each annotated gif into a single gif (where I've reduced the number of frames in the original gif to reduce the file size):

images = 1:4 %>% 
  map(~image_annotate(falls_gif[seq(1,26,4)], paste0("Acre-Feet: ", 2*.x), 
                      size=20, color="white"))

x = image_join(images)
x

preview (4)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.