Group Abstract Group Abstract

Message Boards Message Boards

Showcasing Manipulate[…] via .GIF animations

GROUPS:
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]]
  ]
Let’s 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.
POSTED BY: Vitaliy Kaurov
Answer
1 year ago
Thanks!
POSTED BY: Luca M
Answer
1 year ago

The function, ManToGif, is very interesting to me. It does not work on Mathematica 10.0.2 of Mac OS X Mavericks.
Would you give me a suggestion how to solve this problem?

POSTED BY: Yoshihiro Sato
Answer
2 months ago

Hi Yoshihiro Sato,

I have tried that on MMA 10.0.2 and OS X Mavericks and it is running without any problem. A little slow, but it does run.

Vitaliy, that is very useful. I have been using terminal commands such as convert to generate animated gifs. Promoted by your post I tried to find alternative approaches. I started out trying to use the GUIKit package

Needs["GUIKit`"] 

but I failed miserably. I have, however, an alternative approach to yours. I am not saying that it is better in any way. I am just posting it because I think that the approach is different from yours. Also it runs a bit faster on my machine, it avoids exporting mov or avi files and it gives a lot of control over the slider movement. Here it is:

frames = Flatten@
   Table[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, n1}, -3, 3}, {{q2, m1}, -3, 3}, {{p, {{-1, 0}, {1, 0}}}, {-1, -1}, {1, 1}, Locator}, Deployed -> True, FrameMargins -> 0], {n1, -3, 3}, {m1, -3, 3}];
Export["~/Desktop/frames.gif", frames];

It lacks the elegance of your approach but it gives a video:

enter image description here

It should be easy to see how to change the positions of the locators etc in a predefined way.

Cheers,

M.

POSTED BY: Marco Thiel
Answer
2 months ago

Hi Marco,
I have confirmed the following.
1. The ManToGif works.
2. Your program works faster.
Thanks.
Yoshihiro Sato

POSTED BY: Yoshihiro Sato
Answer
2 months ago

Hey folks, Marco has a great approach - thank you!

There is also a one-liner ala my original version. Use "AVI" for Windows and "MOV" for Mac:

images = ImportString[ExportString[Manipulate[x + y, {x, 0, 1}, {y, 0, 1}], "AVI"], "ImageList"];

This makes the set of images that can be exported to a .GIF:

RandomSample[images, 8]

enter image description here

As you can see all controls are varied. Now tips.

How to define motion of controls. - read the tutorial: Introduction to Manipulate

One reason to care about the details of AutorunSequencing is that it is possible to use the Export command to automatically generate an animation video (in, for example, QuickTime or Flash format). By default Export will generate an animation by running the Manipulate through one Autorun cycle. If AutorunSequencing does not give you enough control over the animation sequence, you can use the Bookmarks feature to define a list of "way points" - combinations of parameter values - and then create an animation that smoothly interpolates through those defined points. This allows complete control over the exact path of the animation. 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. (If no bookmarks are present, the result is one cycle of Autorun.)

To NOT display controls - use "ControlAppearance"->None

Yet another interesting approach. - read docs on VideoFrames

Export["frame001.png", Manipulate[ . . . ], "VideoFrames"]

-- this exports list of frames in a defined format (.PNG in this example). Works in combination with discussed above Manipulate options.

I hope this helps!

POSTED BY: Vitaliy Kaurov
Answer
2 months ago

Dear Vitaliy,

this is indeed very nice and useful. I was trying to implement something where I would have a Manipulate window. A Dynamic or RunScheduledTask would regularly check whether my Manipulate window has changed. If so, it would save a gif frame of the current Manipulate-window and save it into a frame-list. The advantage would be that you can manipulate the sliders directly and whatever you do would be "recorded" by the frame-list. Of course, I could use a program such as GifGrabber to just capture the Manipulate interface. But it is more fun to do it with Mathematica directly.

Cheers,

M.

PS: Did you mean use "AVI" for Mac and "MOV" for Windows?

POSTED BY: Marco Thiel
Answer
2 months ago