# Having trouble producing transparent GIF animation

I'm having difficulty producing a proper animated gif (for inclusion in a PowerPoint presentation). The code I have is this:

 HSBhsl[h_, s_, l_] :=
Module[{H, S, B}, H = h; B = l + s (1 - Abs[2 l - 1])/2;
S = (2 B - l)/B; {H, S, B}]
TestCube[\[Theta]_] :=
Graphics3D[Rotate[{EdgeForm[], Hue[HSBhsl[32/250, 228/250, 105/256]],
Cuboid[{0, 0, 0}, {1, 1, 1}]}, -\[Theta] Degree, {0, 0, 1}, {1, 1,
1}], Lighting -> {{"Ambient",
RGBColor[0.7, 0.7, 0.7]}, {"Directional",
RGBColor[0.35, 0.35, 0.35], ImageScaled[{0, 5, 0}]}},
Boxed -> False, Background -> None,
PlotRange -> 4 {{-1, 1}, {-1, 1}, {-1, 1}}, ViewAngle -> Pi/10000]


The animation is then produced like so:

list = Table[
Show[TestCube[t], ViewPoint -> {1000, 1000, 1000}], {t, 0, 360, 1}];
SetDirectory@NotebookDirectory[];
Export["CubesAnimation.gif", list, "TransparentColor" -> White]


Unfortunately, this produces an amimation that seems to stack images on top of each other, thus giving the appearance of a "trace" left by the previous frame. I tried adding "TransitionEffect" -> Background, but this had no effect.

How do I get a proper GIF animation, with a transparent background, of the rotating cube?

• You haven't defined HSBhsl – bill s Oct 3 '18 at 21:51
• Sorry, updated my post. Thanks! – Pirx Oct 3 '18 at 21:53
• What do you mean by "proper"? Just using Export["CubesAnimation.gif", list] seems to produce a "proper" GIF. – JimB Oct 3 '18 at 23:56
• The issue is that this results in the animation having a white background. I need the background to be transparent. Using the options I gave in my post, I do get a transparent background, but I also get that unwanted "stacking effect". – Pirx Oct 4 '18 at 0:02

I have investigated the problem and it seems that this is indeed a limitation of Export. We will add support for "TransitionEffect" option in Export in the next release of Mathematica.

In the meantime, as a workaround, you can use a low-level function from GIFTools paclet GIFToolsPrivate$WriteFrames. The function is not documented because it is not supposed to be used directly, it serves as a back-end to Export. But here is how you can use it in your case: list = Table[Show[TestCube[t], ViewPoint -> {1000, 1000, 1000}], {t, 0, 360, 1}]; SetDirectory@NotebookDirectory[]; (* Load and initialize GIFTools paclet *) Needs["GIFTools"] GIFToolsPrivateInitGIFTools[] GIFToolsPrivate$WriteFrames[
Image3D[list],           (* All frames stuffed into a single Image3D *)
"CubesAnimation.gif",    (* Output file name *)
"",                      (* Comment, none in this case *)
{0.1},                   (* Display duration for each frame *)
"None",                  (* Dithering method, none in this case *)
1,                       (* How many times to play the animation *)
True,                    (* Interlacing *)
{255, 255, 255},         (* Background color *)
{255, 255, 255},         (* Transparent color - white *)
{2}                      (* Disposal mode - Restore to background color*)
]


As an alternative you can consider exporting to Animated PNG, which supports alpha channel.

• How nice! But, is the semicolon in Image3D[list]; deliberate? – J. M. is slightly pensive Oct 4 '18 at 10:13
• No, it was a typo, I have edited my answer. Thanks for catching that. – rafalc Oct 4 '18 at 10:17
• Thanks, this solves the issue! – Pirx Oct 4 '18 at 11:19
• Unfortunately, Powerpoint doesn't seem to support animated PNGs. – Pirx Oct 4 '18 at 11:35
• Hah, I got it now: It was some issue with cut-and-paste mangling that backwards quote; I did get a character that looks like a backwards quote, but apparently it wasn't the right backward quote. Just manually typing that quote in Mathematica fixed the issue. Thanks everybody! – Pirx Oct 4 '18 at 11:56

To export to an animated PNG with transparency you should convert Graphics to Image first:

list = Table[Image[Show[TestCube[t], ViewPoint -> {1000, 1000, 1000}]], {t,
0, 360, 1}];


Then, you call Export:

In[9]:= Export["CubesAnimation.png", list]

Out[9]= "CubesAnimation.png"

In[10]:= ImageMeasurements[Import["CubesAnimation.png", {"ImageList", 1}], "Transparency"]

Out[10]= True
`