ranimation

Very confused about how to merge two images to create a gif


I have looked at previous posts. Especially this one. I see the code is this

library(animation)
## make sure ImageMagick has been installed in your system
saveGIF({
  for (i in 1:10) plot(runif(10), ylim = 0:1)
})

I installed ImageMagick (after much confusion).

I ran the above code, and do not see anything happening? I have two images on my computer saved in a file. I want to combine them to create a short gif. How do I go about doing this? What portion of the above code combines multiple images that are saved on my computer? How do I go about doing this using the animation library and imagemagick?

I am running into a wall here. Any help will be greatly appreciated.


Solution

  • You can do it using Magick, as follows:

    install.packages("magick")
    library(magick)
    
    list.files(path = "<<path to your images>>", pattern = "*.png", full.names = T) %>% 
      image_read %>% # reads each image file
      image_join() %>% # joins image
      image_animate(fps=2) %>% # animates, can opt for number of loops
      image_write("merged_pngs.gif")
    

    Hope it helps.