Message Boards Message Boards

Showcasing Manipulate[…] via .GIF animations

Posted 11 years ago
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
14 Replies
Posted 7 years ago

hello, Jeremy, You can easily change the speed by adding or removing frames. You can e.g. change the step of the Table function that crestes the frames. here is an example of a fast and a slow version"

framesFast = 
  Flatten@Table[
    Manipulate[
     ContourPlot[
      q1/Norm[{x, y} - {p, 0}] + q2/Norm[{x, y} - {1, 0}], {x, -2, 
       2}, {y, -2, 2}, Contours -> 20, PlotRangePadding -> 0, 
      Frame -> False, PlotPoints -> 40, ImageSize -> 230, 
      ColorFunction -> "DarkRainbow"], {{q1, -1}, None}, {{q2, 2}, 
      None}, {{p, p1}, -1, 1, .1}, Deployed -> True, 
     FrameMargins -> 0], {p1, -1, 1, .25}];
Export["~/Documents/Mathematica Notebooks/Demos in \
Preparation/framesFast.gif", framesFast, "ControlAppearance" -> None]

framesSlow = 
  Flatten@Table[
    Manipulate[
     ContourPlot[
      q1/Norm[{x, y} - {p, 0}] + q2/Norm[{x, y} - {1, 0}], {x, -2, 
       2}, {y, -2, 2}, Contours -> 20, PlotRangePadding -> 0, 
      Frame -> False, PlotPoints -> 40, ImageSize -> 230, 
      ColorFunction -> "DarkRainbow"], {{q1, -1}, None}, {{q2, 2}, 
      None}, {{p, p1}, -1, 1, .1}, Deployed -> True, 
     FrameMargins -> 0], {p1, -1, 1, .025}];
Export["~/Documents/Mathematica Notebooks/Demos in \
Preparation/framesSlow.gif", framesSlow, "ControlAppearance" -> None]

fast gif

slow gif

POSTED BY: Erik Mahieu
Posted 7 years ago

Love this function, thanks for sharing this!

Is there a way for me to slow down the animation? That is, how can I slowdown the rate at which the slider is varied? I understand how to do this with the autorun controls on the manipulate interface, but I am not sure how to do it in the context of exporting.

POSTED BY: Jeremy Binagia
Posted 8 years ago

Got it! Works perfectly now. Thanks. Sorry for the confusion on my part...

POSTED BY: Erik Mahieu
Posted 8 years ago

Thanks,Vitaly.

Point 2. is clear to me. As to point1, I did the following:

frames = Flatten@
   ParallelTable[
    Manipulate[
     ContourPlot[
      q1/Norm[{x, y} - {p, 0}] + q2/Norm[{x, y} - {1, 0}], {x, -2, 
       2}, {y, -2, 2}, Contours -> 20, PlotRangePadding -> 0, 
      Frame -> False, PlotPoints -> 40, ImageSize -> 230, 
      ColorFunction -> "DarkRainbow"], {{q1, -1}, None}, {{q2, 2}, 
      None}, {{p, p1}, -1, 1, .1}, Deployed -> True, 
     FrameMargins -> 0], {p1, -1, 1, .1}];
Export["~/Documents/Mathematica Notebooks/Demos in \
Preparation/frames.gif", frames, "ControlAppearance" -> None];

enter image description here

As you can see, adding None in the Slider specs eliminates the q1 and q2 sliders but the p1 slider stays visible notwithstanding the "ControlAppearance"->None setting. I did not see the example you wanted to add and maybe that will solve my problem?

POSTED BY: Erik Mahieu

You are mixing up two different methods. Look through the whole thread where I "first time" talk about "ControlAppearance" -> None option. There is example below:

Export["file.mov", 
 Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
 "ControlAppearance" -> None]

This will also work:

Export["frame001.png", 
 Manipulate[
  Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], "VideoFrames", 
 "ControlAppearance" -> None]

Both require importing generated files back and exporting as .GIF.

POSTED BY: Vitaliy Kaurov
Posted 8 years ago

Hello, Vitaly, I use your ManToGif a lot. Thanks... But I have some questions:

  1. ControlAppearance->NoneI would like in some cases to hide the controls. You mention: To NOT display controls - use "ControlAppearance"->None. Where and in what function (Export, Manipulate, ?) do I put "ControlAppearance"->None?

  2. I noticed that most GIFs published in the community keep repeating infinitely. Mine do not. Do you have to put AnimationRepetitions->Infinity to get this effect or is it some Export setting?

POSTED BY: Erik Mahieu
  1. "ControlAppearance"->None is an option for Export, I added example.

  2. This depends on the method. When you export a list of images as .GIF it is lopped on default.

POSTED BY: Vitaliy Kaurov

Vitaliy, I love this function, it comes in handy for demonstrating a Manipulate functionality. But I had to modify it a bit to account for the Linux OS, and also I added a line to delete the exported AVI file (which meant I had to make it a Module).

A couple of points: I had to replace OperatingSystem with $OperatingSystem. Then I went ahead and used If instead of Which and === instead of ==, and then I put a default argument for the step because I just personally prefer it to loop forwards then backwards.

ManToGif[man_, name_String, step_Integer: 1] := Module[{moviename},
  moviename = 
   name <> If[$OperatingSystem === "MacOSX", ".mov", ".avi"];
  Export[name <> ".gif", 
   Import[Export[moviename, man], "ImageList"][[1 ;; -1 ;; step]]]; 
  DeleteFile[moviename];
  ]
POSTED BY: Jason Biggs

Very nice, @Jason Biggs, could you share your code? Also have you seen the one-liner above with ExportString and another with "VideoFrames"? - They make things much easier.

POSTED BY: Vitaliy Kaurov

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, for example:

Export["file.mov", 
 Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
 "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

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
Posted 9 years 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

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
Posted 9 years ago

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

POSTED BY: Yoshihiro Sato
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract