Message Boards Message Boards

GROUPS:

Creating an animation in GIF file.

Posted 5 years ago
13211 Views
|
11 Replies
|
3 Total Likes
|

Hi all,

I want to create an animation and export it in a gif file. I am able to export animation in .swf etc. formats but when I tried to export it in gif, it gives me a static picture.

gif=Animate[Plot[Sin[a + x], {x, 0, 10}], {a, 0, 10}]

I want to export it in a gif file.

Export["sin.gif",gif] 

gives me a static pidture. but

Export["sin.swf",gif] 

works fine.

Please help me in creating the animation in gif file.

Attachment

Attachments:
11 Replies
Posted 5 years ago

One way to do it is replace "Animate" with "Table". (But doing it that way the control for "a" will not appear.)

Can you please write down the code? If I can not control the variation of "a", how can it be animated then?

Posted 5 years ago

The Table command creates a list of images. When exporting to "sin.gif" Mathematica sees the ".gif" extension and then knows to create an animated gif from that list of images.

gif = Table[Plot[Sin[a + x], {x, 0, 10}], {a, 0, 10}];
Export["sin.gif", gif]

Thank you so much. This is the exact animated gif file I wanted.

sincerely, Suvadip

Can you tell me how to slow and fast the animation? Because the gif file I have created, is really fast. I want to slow it down.

Hi Suvadip

this is a good question! I would be interested in an answer as well. When I had that problem some time ago, the only solution for me was to adjust the speed of the animation with the help of a regular image manipulation program (e.g. gimp).

Henrik

Posted 5 years ago

For slowing or speeding the animation one can use the "DisplayDurations" option for Export (even though the documentation only mentions this for Import):

(* Export with each frame displaying for 1 second *)
gif = Table[Plot[Sin[a + x], {x, 0, 10}], {a, 0, 10}];
Export["sin1second.gif", gif, "DisplayDurations" -> 1]

1 second duration of each frame

For smoother motion one can add more frames:

(* Smoother motion:  more frames *)
gif = Table[Plot[Sin[a + x], {x, 0, 10}], {a, 0, 10, 0.1}];
Export["sinSmooth.gif", gif]

Smoother motion

For less abruptness when going from the last frame back to the first frame one can consider running the animation backwards to the start (or with something like a sine curve having the last frame be a multiple of the wave length):

(* Remove abrupt ending by going backwards after you reach the end *)
gif = Table[Plot[Sin[a + x], {x, 0, 10}], {a, 0, 10, 0.1}];
Export["sinSmoother.gif",Flatten[{gif, Table[gif[[i]], {i, Length[gif] - 1, 2, -1}]}]]

Images replay in reverse order at the end

In addition, one can add some indication as to the value of the parameter being changed ("a" in this case) with either text or some sort of graphic thermometer:

(* Other enhancements:  add in a "thermometer" or some text that tells you what the value of "a" is *)
gif = Table[Plot[Sin[a + x], {x, 0, 10}, Frame -> True,FrameLabel -> {{"Sin[a+x]", ""}, {"x","a = " <> ToString[a]}}], {a, 0, 10}];
Export["sinText.gif",Flatten[{gif, Table[gif[[i]], {i, Length[gif] - 1, 2, -1}]}]]

Text added to show value of parameter

Hi Nasser,

thank you for your remark! But I tried this long time ago and I found no way to make this work on Linux ... Here I see the only possibility for creating animated gifs in exporting a list of single images.

Henrik

actually what I do to make animated gif files from Manipulate is simply use LICEcap (free). I simply run Manipulate as is, and record it from the screen using LICEcap. Hit the stop button when done recording. It saves the recording as animated gif file. Very simple and no code changes is needed in the notebook. It runs on mac also.

Hi Nasser, hi Jim,

many thanks for your valuable suggestions!

(@Nasser: LICEcap is not running on Linux, but there are similar tools instead, the idea is good nevertheless!)

Henrik

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