18
$\begingroup$

I have the following code to create animated GIFs:

ClearAll["`*"];
xmin = -3;
xmax = 4;
ymin = -10;
ymax = 3;
line1 = Line[{{xmin, ymin}, {xmin, ymax}}];
line2 = Line[{{xmax, ymin}, {xmax, ymax}}]; 
slides = 8; 
cmin = -10; 
cmax = 5/2; 
v = Table[Manipulate[
     Plot[{1/3 x^3 + x^2 + x - (2 x^2 + 4 x), c}, {x, xmin, xmax}, 
      PlotStyle -> {Blue, Red}, AspectRatio -> GoldenRatio, 
      PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
      Epilog -> {Directive[{Green, Green}], line1, line2}, 
      Background -> None], {{c, k/slides, "Constant:"}, cmin, cmax}],  
     {k, cmin*slides, cmax*slides}]; 
v = Join[v[[1 ;; Dimensions[v][[1]] - 1]], Reverse[v]];

When I use:

Export["testManip007.gif", v]

I obtained:

enter image description here

In order to get transparent background gif, I tried the following which worked for other cases:

Export["testManip007t.gif", v, "TransparentColor" -> White]

but the resulted is:

enter image description here

Additionally:

Export["testManip003t.gif", v, "DisplayDurations" -> 0.1, 
 "TransparentColor" -> White]

always causes error message and empty output.

How to solve the problems?

$\endgroup$
3
  • 2
    $\begingroup$ I tried setting Background explicitly to White but with no improvement to the exported gif. I then set both Background and "TransparentColor" to Yellow, which improved the appearance of the slider but not the plot itself. $\endgroup$
    – bbgodfrey
    Nov 18, 2014 at 6:35
  • 1
    $\begingroup$ Setting "Interlaced" option to True or False does not have an effect. $\endgroup$ Nov 18, 2014 at 6:59
  • $\begingroup$ You can't. Animations are realized through frames, i.e., the next frame appears through the former if you set "TransparentColor" -> White. My recommendation stay with white background. $\endgroup$
    – user9660
    Dec 4, 2014 at 17:29

2 Answers 2

16
$\begingroup$

Note for Version 11.1: As commented by LCFactorization the option setting

"TransitionEffect" -> Background 

is no longer working (error message + nothing exported).

The solution is to use "TransitionEffect" -> "Background" instead, which doesn't produce the desired result in version 10.4 and earlier.


By default the frames of a GIF are stacked on top of each other. One can change this behavior using the Export option "TransitionEffect" -> Background:

Export["testManip007t.gif", v, "TransparentColor" -> White, "TransitionEffect" -> Background]

enter image description here

The default setting is "TransitionEffect" -> None.

$\endgroup$
12
  • $\begingroup$ Do you know what other TransitionEffect options are available? From Import[#, {"GIF", {"TransitionEffect"}}] & I have seen only None, False and Background. $\endgroup$
    – rhermans
    Jun 1, 2017 at 16:41
  • 1
    $\begingroup$ @rhermans I didn't come across any other option values either. I'm also not aware of something else the GIF file specifications would provide. It's a relative simple file format and the Mathematica exporter is rather primitive. $\endgroup$
    – Karsten7
    Jun 1, 2017 at 17:58
  • 2
    $\begingroup$ This option seems no longer work in the latest version 11.1? $\endgroup$ Jul 14, 2017 at 13:52
  • 3
    $\begingroup$ @LCFactorization You are correct, thanks for pointing that out. In version 11.1 you have to use "Background" instead of Background as the option value. $\endgroup$
    – Karsten7
    Jul 14, 2017 at 16:29
  • 1
    $\begingroup$ @Karsten7. Hello again! So, I contacted Wolfram Technical Support and they just confirmed that there is no way to export a GIF with a transparent background using the old "TransitionEffect" -> Background trick. They tell me they are "looking to release a new version of Mathematica that has [the "DisposalOperation" option] included in the Export functionality", but right now it only works for importing GIF files. So, I'll have to wait I guess, or maybe reinstall an older version of Mathematica. I'll keep you posted as soon as I learn anything new. And thank you for your help! $\endgroup$
    – RoberRM
    May 21, 2018 at 22:54
1
$\begingroup$

Since Mathematica still seems not to support transparent animated gifs, one can work around the problem by exporting ".png" files and using an external program to stitch them together into an animated gif. For example:

ExportAsTransparentGIF[dir_, basefn_, frames_, ExportOptions___] := 
 Module[{b = 10, n, filename, filenames},
  n = 1 + Round@Log[b, Length[frames]];
  filenames = 
   Table[filename = dir <> basefn <> IntegerString[i, b, n] <> ".png";
     Export[filename, frames[[i]], Background -> None, 
     ExportOptions];
    filename, {i, Length[frames]}];
  command = 
   "/opt/homebrew/bin/convert -dispose 2 -set delay 0 " <> 
    StringRiffle[filenames] <> " " <> dir <> basefn <> 
    "animated.gif";
  Run[command];
  (* cleanup *) 
  command = "rm " <> StringRiffle[filenames];
  Run[command];
  ]

Here I am using the convert command from ImageMagick, installed on my mac with homebrew.

gTetra = Tetrahedron[];
Nt = 150;
frames = Table[t = (2. Pi) i/Nt;
   Show[Graphics3D[
     GeometricTransformation[gTetra, 
      RotationTransform[t, UnitVector[3, 3]]]], 
    Boxed -> False, SphericalRegion -> Sphere[{0, 0, 0}, 0.7]], {i, 
    Nt}];

ExportAsTransparentGIF["~/png-repo/", "Tetra-spin-", frames];

enter image description here

$\endgroup$

Your Answer

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

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