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]]
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 Export
ing 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!):
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 Export
ing 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
Export
frames into GIF. Export
ing 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$
Commented
Jun 10, 2015 at 13:50
GIF
export speed truly is awful. $\endgroup$Export
to"VideoFrames"
as I mention in this answer, and assemble the gif in an external tool such asImageMagick
orGraphicsMagick
, or evenffmpeg
. 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 toGIF
export... $\endgroup$GIF
export, too. In particular, it helps to choose the image size and dimensions at the rasterizing stage, not in theGIF
export. $\endgroup$