Writing GIFs and videos from JPGs using Magick is slow and of poor quality

219 Views Asked by At

I often write something like 300 frames that I want to make into a 20 second animation using the magick package in R. I am finding that to write the frames to a 300 jpegs and then bringing them into photoshop is more reliable than working with either image_write_gif or image_write_video. Nevertheless, I would like to do everything in R and hope that someone has a suggestion for something more robust.

There is a similar question "slow or failed writing image vector to *.gif with magick in R?" That question does not include a reproducible example and has not been answered.

I have experimented with image_write_gif and image_write_video as well as image_write. It takes about 10 times longer to write the gif and write the video than to just write the images. Also, in the examples below you get a smooth gradation of grey in the individual images, but the graduation breaks down a bit in the gif and the mp4 and there is some flickering.

library(magick)
pic1 <- image_blank(600, 600, pseudo_image = "gradient:white-black")
pic1 <- image_convert(pic1, format = "jpeg")
stack <- image_morph(c(pic1, image_negate(pic1)), frames = 100)
length(stack)

a <- Sys.time()
for (i in 1:length(stack)) {
  image_write(stack[i], path = paste0("test", i, ".jpg"))
}
b <- Sys.time()
b - a

a <- Sys.time()
image_write_gif(stack, "test.gif")
b <- Sys.time()
b - a

a <- Sys.time()
image_write_video(stack, "test.mp4")
b <- Sys.time()
b - a

The first step (writing the individual jpegs takes about a half a second. Writing the gif takes 6 seconds and the mp4 takes 4 seconds on my MacBook that is a 2018 vintage.

0

There are 0 best solutions below