Note that there are some explanatory texts on larger screens.

plurals
  1. POMathematica: Transparent background with PNG
    text
    copied!<p>This is most likely a bug in Mathematica 8.0.1 and maybe other versions too. Lets try the following:</p> <pre><code>Table[ Export[ "Res_" &lt;&gt; ToString[r] &lt;&gt; ".png", Rasterize[ Style[x^2 + y^2, 40], Background -&gt; None, ImageResolution -&gt; r ], Background -&gt; None], {r, {20, 40, 100, 300, 400, 500, 600}} ] </code></pre> <p>This is a screen shot of what I obtain:</p> <p><img src="https://i.stack.imgur.com/PejVz.png" alt="Output"></p> <p>First thing to notice is that the last two pictures are the wrong size. This is fine to some extent since I'm satisfied with a resolution of 300 or above. Now look at this:</p> <pre><code>in = 72; G3D = Graphics3D[AspectRatio -&gt; 0.925, Axes -&gt; {True, True, True}, AxesEdge -&gt; {{-1, -1}, {1, -1}, {-1, -1}}, AxesStyle -&gt; Directive[10, Black], BaseStyle -&gt; {FontFamily -&gt; "Arial", FontSize -&gt; 12}, Boxed -&gt; False, BoxRatios -&gt; {1, 1, 1}, LabelStyle -&gt; Directive[Black], ImagePadding -&gt; All, ImageSize -&gt; 5 in, PlotRange -&gt; All, PlotRangePadding -&gt; None, TicksStyle -&gt; Directive[10], ViewPoint -&gt; {2, -2, 2}, ViewVertical -&gt; {0, 0, 1}, Background -&gt; None ]; surf = Show[ Graphics3D[Sphere[{0, 0, 0}, 1], Background -&gt; None, AxesLabel -&gt; {"x", "y", "z"}], Options[G3D] ]; fig = Show[surf, AxesStyle -&gt; Directive[Opacity[0]], Background -&gt; None ]; </code></pre> <p>I wish to <code>Export</code> fig as a png file with transparent background with a high resolution. Here goes my lame attempt with the always buggy Mathematica.</p> <pre><code>Table[ Export[ "Res_" &lt;&gt; ToString[r] &lt;&gt; ".png", Rasterize[fig, ImageResolution -&gt; r, Background -&gt; None], Background -&gt; None ], {r, {20, 40, 100, 300, 400, 500}} ] </code></pre> <p>Here is a screenshow of a few png files.</p> <p><img src="https://i.stack.imgur.com/RPQff.png" alt="Images"></p> <p>All of them came out with the expected resolution :). But what happened to my transparent background? I have specified many times through my code <code>Background -&gt; None</code> and yet this doesn't want to work. I looked around the web and I found this:</p> <p><a href="http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00943.html" rel="nofollow noreferrer">http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00943.html</a></p> <p>Lets use this idea.</p> <pre><code>bgImage = Image[ConstantArray[{0, 0, 0, 0}, Reverse[ImageDimensions[fig]]], ColorSpace -&gt; "RGB"]; compImage = ImageCompose[bgImage, fig]; Table[Export["Res_" &lt;&gt; ToString[r] &lt;&gt; ".png", Rasterize[compImage, ImageResolution -&gt; r, Background -&gt; None], Background -&gt; None], {r, {20, 40, 100, 300, 400, 500}}] </code></pre> <p><img src="https://i.stack.imgur.com/kSaGb.png" alt="Images"></p> <p>No backgrounds!!! :) Great. But what happened to the sizes of my images? The resolution is increasing but the image size started to decrease. I have really been messing around with this problem for too long now. I hope one of you can shed some light into this Mathematica bug and can find a hack in order to achieve a transparent background PNG with high resolution. Please mention the Mathematica version you guys are using if you find an answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload