When we create Manipulate or Animate and would like to showcase them on Wolfram Community site, a cool way is to make an animated GIF file of it. It would be also great if this GIF file could contain motion of controls, so people can see what they do. We can apply Export function to Manipulate with .AVI or .MOV or .FLV formats. This will make a movie that can show all motions of controls and content of Manipulate. But from a movie to a GIF there are just a few steps. This function below does these steps. It basically exports Manipulate to a movie, imports it as ImageList, and then exports it again as a GIF. Because Mac and Windows have different native movie formats we need to auto-detect them. The result of the function is two files one is a movie and another is an animated GIF saved in default directory. Here is the legend for arguments:
- man - variable representing Manipulate
- name - pure name of the file without any extension
- step - which every frame to pick: 1 - original no compression, 2 every 2nd compress twice, etc.
ManToGif[man_, name_String, step_Integer] :=
Export[name <> ".gif",
Import[
Export[name <> Which[$OperatingSystem == "MacOSX", ".mov", $OperatingSystem == "Windows", ".avi"],
man],
"ImageList"][[1 ;; -1 ;; step]]
]
Lets see how it works on an example. Here is a Manipulate with 4 controls: 2 sliders and 2 locators.
man = Manipulate[ContourPlot[
q1/Norm[{x, y} - p[[1]]] + q2/Norm[{x, y} - p[[2]]], {x, -2,
2}, {y, -2, 2}, Contours -> 20, PlotRangePadding -> 0,
Frame -> False, PlotPoints -> 40, ImageSize -> 230,
ColorFunction -> "DarkRainbow"], {{q1, -1}, -3, 3}, {{q2, 2}, -3,
3}, {{p, {{-1, 0}, {1, 0}}}, {-1, -1}, {1, 1}, Locator},
Deployed -> True, FrameMargins -> 0];
Here is the result of the function:
ManToGif[man, "charge", 2]
Any suggestions how we can improve this function?
- To make it work faster
- To make smaller .GIFs
- Any other way
P.S. - A few things to note:
- Control the screen size of GIF by controlling size of Manipulate content.
- The smaller the screen size, the smaller the byte size.
- By default Export will generate an animation by running the Manipulate through one Autorun cycle.
- AutorunSequencing is used when a Manipulate expression is exported using Export to a dynamic format.
- Use AutorunSequencing to specify how autorun should use the controls provided.
- When a Manipulate output containing explicit bookmarks is exported to a video animation format using Export, the resulting video will be one cycle through the sequence generated by Animate Bookmarks.