yihui / animation

A gallery of animations in statistics and utilities to create animations
https://yihui.org/animation/
206 stars 60 forks source link

Support converting to gif using 'magick' package, closes #85 #114

Closed jeroen closed 6 years ago

jeroen commented 6 years ago

See discussion in https://github.com/yihui/animation/issues/85. Use the 'magick' package to convert to gif, which does not require installing and shelling out to im/gm executables.

This prevents all the problems with finding the path to convert and escaping shell arguments. It is a bit faster too. Here is a quick benchmark:

test <- function(){
  for(i in 1:10)
    plot(rnorm(i))
}

library(animation)
library(microbenchmark)
microbenchmark(
  magick = saveGIF(test(), convert = 'magick'),
  convert = saveGIF(test(), convert = 'convert'),
  times = 20)

## Unit: milliseconds
##    expr      min       lq     mean   median       uq      max neval cld
##  magick 443.3866 457.8364 477.4666 473.2199 481.5450 582.4752    20  a 
## convert 534.5049 553.7037 569.6321 566.7770 575.9479 664.3616    20   b
yulijia commented 6 years ago

I cannot run this example examples/spinner-ex.R, do you know how to pass any extra.opts to magick package?

jeroen commented 6 years ago

It depends on which option you're trying to set. The image_animate() has a dispose argument, I think that's what you're using in this example?

yulijia commented 6 years ago

Do you know if there is a better way to implement extra.opts with image_animate()? It has been a long time ago, since I try to use image_animate() to instead of calling ImageMagick using shell. But I want to keep provide extra.opts in this function, users can call other ImageMagick parameter such as dispose, resize, and colors in it.

jeroen commented 6 years ago

I added automatic parsing of the -dispose parameter from the extra.opts argument vector.

Which other image transformations do you need to support? Afaik the size and colors of the images are set via the graphics device parameters rather than the png -> gif conversion?

yulijia commented 6 years ago

Have you tested the example above? I still cannot run it because this ani.dev = function(...) png(bg = "transparent", ...)

Error in as.character(pattern) : cannot coerce type 'closure' to vector of type 'character'

extra.opt supports user to do some image transformations like --colors 256, -coalesce, and -resize 300x400.

I guess the yihui provides extra.opts because some users want to adjust images and generate the gif in the same time.

yulijia commented 6 years ago

@yihui Could we remove extra.opts? if we only accept parameters in image_animate(), we can avoid ImageMagick dependencies problem, and other small problems like system -> system2 (#111) .

yihui commented 6 years ago

@yulijia I'm fine to remove it if we didn't use it anywhere in this package. A safer strategy is to document the limitation of this argument in case of convert = 'magick'.

jeroen commented 6 years ago

@yulijia I think the example was broken already before this PR. I am getting the same error in the master branch.

yulijia commented 6 years ago

@jeroen Sure, I just figure out what happened. Sorry for the disturb.

jeroen commented 6 years ago

Keeping the dispose parameter is useful because it is part of the animation procedure. However other parameters are general image transformations, they don't need to be part of the png -> gif function.

yulijia commented 6 years ago

I totally agree. Thank you @jeroen , you help me a lot.