5
$\begingroup$

My Export is taking a very long time, I was hoping there might be a way to monitor the progress when exporting graphics?

AbsoluteTiming[Export["timeline.gif", table, ImageSize -> 1000]]
$\endgroup$
  • 3
    $\begingroup$ I feel with you - GIF export speed truly is awful. $\endgroup$ – Yves Klett Jun 10 '15 at 6:30
  • $\begingroup$ What you could do is to Export to "VideoFrames" as I mention in this answer, and assemble the gif in an external tool such as ImageMagick or GraphicsMagick, or even ffmpeg. Then you have the direct "progress indicator" in the form of the file listing in your directory (i.e., you can watch the directory filling up with the individual video frames as they are created). I won't write this as an answer because it's just an alternative to GIF export... $\endgroup$ – Jens Jun 10 '15 at 16:08
  • $\begingroup$ In the same answer linked above, I also mention that you get faster results if you first rasterize the image list before exporting. That helps with GIF export, too. In particular, it helps to choose the image size and dimensions at the rasterizing stage, not in the GIF export. $\endgroup$ – Jens Jun 10 '15 at 16:11
10
$\begingroup$

Mathematica's Export of animated GIFs isn't perfect. I personally prefer to Export only individual frames from Mathematica, and then use a third-party program for assembling them into an optimized animated GIF. In this case it is easy to monitor the process of Exporting of individual frames:

table = Table[
   Plot[Sinc[t x], {x, 0, 10}, PlotRange -> 1], {t, 0, 10, .1}];

n = Length[table];
CreateDirectory["timeline.gif (frames)"]
PrintTemporary[ProgressIndicator[Dynamic[k], {1, n}]];
Do[Export[
  "timeline.gif (frames)/frame_" <> IntegerString[k, 10, 4] <> ".gif",
   table[[k]]], {k, n}]

Caveat: In the above code I Export frames into GIF for compatibility with free Gifsicle utility (see below) which can read only GIF. The drawback is that every frame has its own palette of colors which is limited for GIF to at most 256 colors. When these frames will be combined into one optimized animated GIF a new color palette (also limited to at most 256 colors) will be generated which will be common for all the frames. It is clear that this process should degrade the quality of most of the frames. In order to avoid this effect I recommend to Export frames into more rich image format like PNG or (even better) TIFF and then use a professional tool like GIF Movie Gear for assembling frames into one optimized GIF having the best possible quality with smallest possible size.


The following is an example (works under MS Windows but can be easily adapted for other OSs) of how the creation of the animated GIF can be automatized using the free Gifsicle utility (I intentionally do not close the console window for having an ability to see the messages generated):

wrapByQuotes[s_String] := "\"" <> s <> "\"";
pathToGifsicle = "D:\\PROGRAMS\\gifsicle\\gifsicle.exe";
framesPath = FileNameJoin[{Directory[], "timeline.gif (frames)", "*.gif"}];
outputFilePath = FileNameJoin[{Directory[], "timeline.gif"}];
command = 
  wrapByQuotes[
   StringJoin[wrapByQuotes@pathToGifsicle, 
    " --delay=10 --loop --optimize=3 ", 
    wrapByQuotes@framesPath, 
    " --output ", wrapByQuotes@outputFilePath]];
Run["start cmd /K", command]

The result is a file of size 182 Kb (compare to 290 Kb size of file generated by Mathematica!):

gif

P.S. Information on subtleties of using of the Run command can be found in this answer by Rui Liu (Wolfram Technical Support).


From the other side, the GIF Export time in Mathematica seems to be a linear function of the number of frames, so after some initial benchmarking it is possible to predict the approximate time needed for Exporting of a GIF with given number of frames:

table = Table[
   Plot[Sinc[t x], {x, 0, 10}, PlotRange -> 1], {t, 0, 10, .1}];
nMax = 60;
First@AbsoluteTiming[
  timings = 
    Table[First@
      AbsoluteTiming[Export["timeline.gif", Take[table, k]]], {k, 1, nMax}];]

lm = LinearModelFit[timings, x, x];
Show[Plot[lm["Function"][n], {n, 1, nMax}], ListPlot[timings], 
 Frame -> True, Axes -> False, 
 Epilog -> Text[lm["Function"][N], ImageScaled[{.5, .9}]], 
 FrameLabel -> {"Number of frames N", "Time, seconds"}]

174.896

plot

$\endgroup$
  • $\begingroup$ great tool to share. $\endgroup$ – Kattern Jun 10 '15 at 7:36
  • $\begingroup$ On Linux, after finally losing my patience over exporting an animation I decided to try if imagemagick could be called within Mathematica; this proved quite satisfactory. $\endgroup$ – J. M. is away Jun 10 '15 at 11:35
  • 1
    $\begingroup$ @Guesswhoitis. Good point about imagemagick: it can read images in any format, not only GIF what removes the restriction to Export frames into GIF. Exporting into more rich image format is preferred in order to obtain the best optimized color palette which will be common for the all frames in the final animated GIF. So better solution would be to Export frames into PNG (or even TIFF) and then assemble them into animated GIF using a professional tool like imagemagick. I have very good experience with GIF Movie Gear. $\endgroup$ – Alexey Popkov Jun 10 '15 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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