3

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.

DiscoR
  • 247
  • 2
  • 11

1 Answers1

2

You can do it using ImageMagick, 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.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • THIS WORKED! Thanks a tonne! Do you have any idea whats going on with the above code? Or what that person was trying to do in the linked webpage? Don't worry about it is too much of a hassle. – DiscoR Nov 21 '18 at 00:05
  • Glad I could help. That one is actually when you don't have image files already. It generates multiple plots and creates a *.gif* file right away. – Taher A. Ghaleb Nov 21 '18 at 00:26