Search code examples
rvideogif

Create GIF or video from external jpg in R


I wanted to do something I thought simple, but... well, I failed repetitively.

I want to create a gif or a video (.avi) from pictures in a folder with R. I can list the path and the names of the pictures (e.g. "./folder/1.jpg" "./folder/2.jpg" "./folder/3.jpg" "./folder/4.jpg" )

I just wanted to put them the one after the other and create a video or gif file (I will treat them frame by frame later, so the speed is not important)

I found a solution with SaveGIF, it works with plots in R but I didn't find the way to use it with external jpg.

Otherwise, there was this solution with image_animate "Animated graphics", but again, I didn't manage.

Do somebody already have a solution to do that? Thank you very much for your help!


Solution

  • You can do it with the magick package, which gives you access to ImageMagick functions. For example, if the frames of your movie are in files named

    frames <- paste0("folder/", 1:100, ".jpg")
    

    then you would create a movie using

    library(magick)
    m <- image_read(frames)
    m <- image_animate(m)
    image_write(m, "movie.gif")
    

    You could choose to write to other formats as well, just by changing the filename, or using other arguments to image_write().