I have a bunch of png files in a directory and I want to convert them into a gif (animated) file via R. Can you please advise how to do that?
If you use Google Photos on Android (or iOS), you can make an animated GIF from a selection of your pictures. Just tap Library, then Utilities and Create New. Choose Animation, select the photos and tap Create.
Here is some dummy code you can use:
First use the magick
package for the GIF
Use the magrittr
package or the dplyr
package for the %>%
library(magick)
library(magrittr)
Then list files in the directory, and combine into gif fps is frames per second
list.files(path='/$PATH/', pattern = '*.png', full.names = TRUE) %>%
image_read() %>% # reads each path file
image_join() %>% # joins image
image_animate(fps=4) %>% # animates, can opt for number of loops
image_write("FileName.gif") # write to current dir
A solution with the gifski
package:
library(gifski)
png_files <- list.files("path/to/your/pngs/", pattern = ".*png$", full.names = TRUE)
gifski(png_files, gif_file = "animation.gif", width = 800, height = 600, delay = 1)
The advantage of gifski
is that the number of colors in the GIF is not limited to 256.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With