ropensci / magick

Magic, madness, heaven, sin
https://docs.ropensci.org/magick
Other
456 stars 63 forks source link

Coalesce not an available option #289

Open caewok opened 3 years ago

caewok commented 3 years ago

Not sure if this is a feature request or a bug.

I would like to be able to coalesce an imported animation. For example, convert koala.gif -coalesce xx_%05d.png using the command line creates a set of complete pngs for each point a the animation. Importing the gif in the magick package instead results in subframes, and I don't see a comparable way within magick to get a full set of images from the imported animation.

Relatedly, there is an image_coalesce function in the package reference, but that function is not actually exported. And when I try to use magick:::image_coalesce on an imported gif, I still end up with subframes. So I don't think that image_coalesce function is intended to be used in this manner. Instead, it seems like magick::image_convertand/or magick::image_read should take a coalesce parameter and modify the imported images accordingly. Thanks!

jeroen commented 3 years ago

Does image_animate(img, optimize = FALSE) do what you want? Perhaps icw a custom method parameter? See:

https://github.com/ropensci/magick/blob/d6b766b0388bcd3cd85115ec2315666691a1fa25/src/animation.cpp#L8-L19

caewok commented 3 years ago

No, image_animate does not do the trick. Take this koala gif, for example. When I run the following code, it imports the gif, but because the gif was optimized, it does not fill in the subsequent frames with the entire image. So the animation only shows the changing parts of the image:

image_read("koala.gif") %>%
  image_animate(delay = .1, optimize = FALSE)

You can also see this when you place the frames side-by-side:

image_read("koala.gif") %>%
  image_append()

FYI, setting optimize to TRUE in image_animate makes no difference. The problem seems to be that image_animate needs to start with the full set of images, and then optimize can remove unchanging pixels when animating the images. But not the other way round.

jeroen commented 3 years ago

I think what you want is image_animate(optimize = FALSE, dispose = 'none')

caewok commented 3 years ago

Yes,image_animate(optimize = FALSE, dispose = 'none') seems to work. Thanks! I think it would still be worth adding a coalesce option to image_read or convert, because that would be closer to the expected workflow in the command-line version of magic.