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"]]

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!

  • 1
    $\begingroup$ I have edited my answer, hopefully it's more helpful now. $\endgroup$ – rhermans Aug 1 '18 at 10:37

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).

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.

  {#, 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.


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

  n = Query["ImageCount"]@GIFTools`Private`$ReadFileMetadata[path]
  GIFTools`Private`$ReadOneFrame[path, j]
  , {j, 1, n, 1}
  • 2
    $\begingroup$ All these operations get exponentially worse, though, as the GIF size increases. The importer is terrible and extracts the entire GIF stack even to read things like metadata. $\endgroup$ – b3m2a1 Jul 31 '18 at 23:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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