1

I have a gif Example.gif, and would like to play it in a presentation. I used the following code:-

a = Import[StringJoin[NotebookDirectory[], "Example.gif"]]
ListAnimate[a]

But it is very slow (waiting time more than 30 seconds up to 1 minute). As I have several gif, the total waiting time is significant. How can I make it faster and smoother to play, just like normal built-in image explorer of the OS? I tried to change the first line from Import to the list of images of the gif (that means I have already stored the gif inside the notebook, instead of importing it), but it doesn't help. When I run ListAnimate[a], I still need to wait for the "Formatting Notebook Contents" for a significant duration before the gif is played.

enter image description here

How can I get it done? Many thanks!

H42
  • 3,469
  • 7
  • 17

2 Answers2

1

I get an animation in less than 3 seconds doing

Import[path, "Animation"]

plus 1 second or 2 for notebook formatting.

Most of the operations give similar timings for a 1.1 MB "GIF" file.

TableForm[
  {#, First@AbsoluteTiming[Import[path, #]]} & /@ 
  Import[path, "Elements"]
 ]

Mathematica graphics

After the comment by @b3m2a1 linking to this answer, it seems this should be considerably faster.

ListAnimate@
 Image3DSlices@GIFTools`Private`$ReadAllFrames[path]

or maybe read only the frames that are needed as they are needed

With[
 {
  n = Query["ImageCount"]@GIFTools`Private`$ReadFileMetadata[path]
  },
 Manipulate[
  GIFTools`Private`$ReadOneFrame[path, j]
  , {j, 1, n, 1}
  ]
 ]
rhermans
  • 36,518
  • 4
  • 57
  • 149
1

If I understand the question correctly, the purpose is to be able to show animations during a presentation without having to wait for them to load or be generated.

What I would do is to create a master notebook that produces the animations, and then a presentation notebook that only displays the animations.

  • After running the producer notebook, click on the Cell bracket for an animation.
  • Copy the cell to the display notebook.
  • The copied cell contains the animation embedded as compressed data (unfortunately not very memory efficient, see How to place an image inside a notebook, with the minimum memory footprint?)
  • The display notebook can be saved and evaluated as usual, it won't affect the animation output cells you pasted into it (they're not evaluatable).
Jens
  • 97,245
  • 7
  • 213
  • 499