17
$\begingroup$

I find it very convenient to use Mathematica notebooks to organize the codes + results.

I was wondering if it is possible to insert an animated image (say an animated gif) in the middle of a notebook.

I tried dragging and dropping an animated gif into a notebook, the gif itself was created using Mathematica. Mathematica imports it and I see a bunch of images in the notebook. This suggests one solution: using a Manipulate or Animate, with those bunch of images. But is there a less tedious and more elegant way?

$\endgroup$
2
  • 8
    $\begingroup$ Import["pp.gif", "Animation"] ? $\endgroup$ Mar 15, 2012 at 18:32
  • $\begingroup$ fyi, I have asked the same question here forums.wolfram.com/mathgroup/archive/2012/Feb/msg00070.html Is there a trick to embed an animated gif file in a Mathematica notebook? (The gif file do not have to 'run' while inside the notebook, I am only interested in the HTML export version this was in Feb 2012 but there was no answer. notebook?` $\endgroup$
    – Nasser
    Aug 14, 2013 at 18:11

3 Answers 3

12
$\begingroup$

The credits go to belisarius and the Mathematica help (ref/format/GIF), but I thought the comment would be worth an answer.

Import["ExampleData/cellularautomaton.gif", "Animation"]

Mathematica graphics

$\endgroup$
9
$\begingroup$

Unfortunately I don't know of any simple and convenient ways.

You can import the GIF first:

frames = Import["http://i.imgur.com/ivfdq.gif"];

Then you can use ListAnimate to get an animation:

ListAnimate[frames]

For completeness I'll mention that pre-6 versions could also animate. Scan[Print, frames] will give you each frame in separate cells. Now close the cell group by double clicking its bracket and press Ctrl-Y or Ctrl-Shift-Y to start the animation. This still works in version 8.

$\endgroup$
3
  • $\begingroup$ (Slightly OT): Is there some way to import the animated GIF and have it appear animated -- but without the frame and Animator element? $\endgroup$
    – murray
    Mar 20, 2012 at 18:57
  • $\begingroup$ @murray Well, you can always hack together something manually ... list = Range[100]; DynamicModule[{i = 1}, Dynamic[Refresh[list[[i = Mod[i + 1, Length[list], 1]]], UpdateInterval -> 1/4], TrackedSymbols -> {}] ] $\endgroup$
    – Szabolcs
    Mar 21, 2012 at 9:23
  • $\begingroup$ @murray Try Dynamic@frames[[Clock[{1, Length@frames, 1}, Length@frames/30.]]] or perhaps less satisfactorily ListAnimate[frames, Paneled -> False] /. HoldPattern[AppearanceElements -> _] -> AppearanceElements -> None -- Oops, I just noticed how old this is...oh, well. $\endgroup$
    – Michael E2
    Aug 14, 2013 at 18:21
7
$\begingroup$

Here is one more attempt to get almost the exact appearance of an animated GIF without the playback controls. I don't recommend putting too many of these into a notebook, though:

makeAnimation[list_, delayList_: {.03}] := 
 DynamicModule[{l = Length[list], delays = Abs@Flatten[{delayList}], 
   times, totalTime, delta = .03, frames}, 
  times = Round[.5 + PadRight[delays, l, delays]/delta];
  frames = 
   Flatten@Table[Table[list[[i]], {times[[i]]}], {i, Length[times]}];
  totalTime = Length[frames];
  EventHandler[
   Dynamic[frames[[Clock[{1, totalTime, 1}, totalTime delta]]], 
    TrackedSymbols -> {}], {"MouseUp", 2} :> Null]]

makeAnimation[
 Import["ExampleData/cellularautomaton.gif", "ImageList"]]

gif

You can copy this animation by selecting its cell bracket. It's persistent across notebooks, too.

$\endgroup$
2
  • $\begingroup$ Pretty slick. +1 $\endgroup$
    – Mr.Wizard
    Aug 14, 2013 at 18:33
  • $\begingroup$ Nice. But the animation is not seen in the HTML exported file. That is what I really wanted all along. Here is screen shot !Mathematica graphics and here was my original question on this forums.wolfram.com/mathgroup/archive/2012/Feb/msg00070.html it would be nice if M can use the animation as animated gifs when exporting to HTML and also PDF (since PDF can have animations in them these days). $\endgroup$
    – Nasser
    Aug 14, 2013 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.