7
$\begingroup$

There is similar question here exporting-animated-gifs-animationrepetitions-1-doesnt-work

But the accepted answer there no longer works in 11.3, which is to use "AnimationRepetitions" -> 0 as it gives error

Export::erropts: The value 0 specified for the option AnimationRepetitions is invalid.

I need the animated gif file to play once and stop. By default, with no options, the animation in the Chrome browser always plays 2 times and then stops. I also tested it in FireFox.

The help page on AnimationRepetitions does not say anything about how to make it play only once. Using "AnimationRepetitions" -> 1 has no effect also, it plays 2 times always then stops. Has this changed in 11.3 from the time of the accepted answer above?

Here is MWE

SetDirectory[NotebookDirectory[]];
r=Table[
   Grid[{{Row[{"time ",t}]},
   {
     Plot[Sin[t x],{x,-2 Pi,2 Pi}]
   }}],
   {t,0,10,0.5}
];
Export["anim0.gif",r,"AnimationRepetitions"->1]

loading the above animation to the browser (drag the file and drop it on a web page that is open), will show it plays 2 times.

question: How to make it play only once in 11.3? Is there other options to use? Version 11.3 on windows 7 home edition.

$\endgroup$
1
  • 2
    $\begingroup$ I suspect a bug. It seems that the exported GIF animation always plays "AnimationRepetitions"$+1$ times in my MMA 11.3, and as you said, a value of $0$ for "AnimationRepetitions" is not accepted. $\endgroup$
    – MarcoB
    Commented Apr 23, 2018 at 15:02

1 Answer 1

8
$\begingroup$

This is indeed a bug in 11.3 and there is no way to export a gif that will only play once. That bug will be fixed in the next release.

Here is a simple workaround you can use:

ExportSingleAnimationGIF[fname_String, expr_, opts___] := 
With[{
    bytes = StringToByteArray[ExportString[ expr, "GIF", opts, CharacterEncoding -> "ISO8859-1"], "ISO8859-1"],
    loop = {33, 255, 11, 78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48, 3, 1, _, _, 0}
},
    With[{
        gifBytes = Join @@ SequenceSplit[Normal[bytes], loop]
    }, 
        BinaryWrite[fname, gifBytes];
        Close[fname];
        fname
    ]
]

You call it with the same arguments and options as you would pass to Export but obviously "AnimationRepetitions" will be ignored. It should always produce a GIF that only plays once.

$\endgroup$
1
  • $\begingroup$ What changes do I need to do to make the animation loop indefinitely? I have version 11.2. In my browser the animation runs exactly twice. Irrespective of the value I give to "AnimationRepetitions". If you prefer, I can write another question about it. $\endgroup$ Commented May 17, 2021 at 12:55

Your Answer

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

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