Is there a way to make GIFs in R without the use of ImageMagick or other third party software?

621 Views Asked by At

I have been attempting to generate a GIF from plots generated and saved in R. With the current system we have in place using ImageMagick comes at a great inconvenience. Is there any other ways or packages native to R that can create GIFs? I have researched on stack overflow and have only found responses involving ImageMagick.

2

There are 2 best solutions below

0
On

There's no R built-in way to make gifs (to my knowledge) that doesn't rely on an external library to convert images saved as single frames into a gif.

For that conversion ImageMagick is probably most common, but there are other tools you could use as well. The animation package system requirements suggests you could alternatively use it with GraphicsMagick or even LyX, or you could manually (or programmatically if you can roll your own API) use any number of online tools.

0
On

You don't mention your operating system, nor whether you mean simple, single-frame GIFs or animated GIFs. If you just want simple GIFs, you could use the NetPBM package which is much lighter weight and easier to install than ImageMagick.

So, supposing you have a PNG file called input.png and you wanted to convert it to a GIF called result.gif, you could do:

pngtopam < input.png | pamtogif > result.gif

If you want to do that from inside R, you would need to use the system() command:

system("pngtopam < input.png | pamtogif > result.gif")

As you can probably see, NetPBM converts all inputs to its own, standard, lowest common-denominator PAM format and from there onwards to all its known output formats, thereby needing only 2N conversions between the N formats it supports rather than N^2 conversions - sweet!

So, for 64 formats:

64^2=4,096   (conversions to write)

whereas

2x64=128     (conversions to write)