Can a wireframe plot made with mathematica be turned into a transparent image and then exported to another site? The reason for this question is that I am trying to fit the wireframe to a structure.
-
1In what format do you want to export it? – Dr. belisarius Oct 13 '11 at 20:46
-
@belisarius I guess to a format that supports transparency. AFAIK that would be GIF or PNG, both supported by mma. – Sjoerd C. de Vries Oct 13 '11 at 20:50
-
@Sjoerd But that does not seem what you need to `fit the wireframe to a structure` – Dr. belisarius Oct 13 '11 at 20:51
-
@belisarius I assumed the OP wants to place the image as an overlay on another image of some type of structure. May be mistaken here. – Sjoerd C. de Vries Oct 13 '11 at 20:57
-
@Sjoerd if you need to superimpose a wireframe to a structure you need a 3D representation, but `May be mistaken here` – Dr. belisarius Oct 13 '11 at 22:30
-
This may be related: http://stackoverflow.com/questions/6353337/mathematica-3d-wire-frames – jmlopez Oct 14 '11 at 21:45
-
If a 3D format is required, it is worth noting that not all of them will work with the example Mr.Wizard gave. DXF, WRL and X3D do, but some of the others listed in the Mathematica documentation don't for this type of `Graphics3D` object. – Verbeia Oct 15 '11 at 00:27
2 Answers
The following code allows to export to GIF with transparent background (based on Mr.Wizard's code):
g = Plot3D[{x^2 + y^2, -x^2 - y^2}, {x, -2, 2}, {y, -2, 2},
RegionFunction -> Function[{x, y, z}, x^2 + y^2 <= 4],
BoxRatios -> Automatic, PlotStyle -> None, Axes -> None,
Boxed -> False]
Export["wireframetest.gif", g, Background -> White,
"TransparentColor" -> White]
Here is how it is displayed in the standard "Windows Picture and Fax Viewer" of Windows XP:
Note that in this case you have to specify both background for Graphics3D
(which is White
by default) and the color which will be converted to transparent color when exporting to GIF. So one should be careful in the case when the target image should contain non-transparent white pixels: Export
not only completely ignores the alpha-channel of the original image but also considers equal RGB values which differ less than by 1/256.4971 (checked on 32 bit Windows XP with Mathematica 8.0.1):
Rasterize[Graphics[{RGBColor[0, 0, 0], Disk[]}]] ===
Rasterize[Graphics[{RGBColor[0, 0, 1/256.4971], Disk[]}]]
(*=> True*)
One way to deal with this situation is to specify a non-standard color for the background which does not appear in the rendered version of the graphics:
Export["wireframetest.gif", g,
Background -> RGBColor[1, 1, 0.996],
"TransparentColor" -> RGBColor[1, 1, 0.996]]
This approach can be automatized. First of all, we can get all the colors which appear in the rendered version of the image:
neededColors = Union[Flatten[Rasterize[g, "Data"], 1]]
At the second step we should select a color which does not appear in this list (the following is a quick and dirty solution):
colorForBackground =
RGBColor @@ (First@
Complement[Permutations[Range[0, 255], {3}], neededColors]/255)
Now we can use it as follows:
Export["wireframetest.gif", g, Background -> colorForBackground,
"TransparentColor" -> colorForBackground]

- 9,355
- 4
- 42
- 93
-
@Mr.Wizard I am amazed that *Mathematica* is limited to 256 colors for each of the RGB channels! It is not what I expected from such comprehensive system. :( – Alexey Popkov Oct 15 '11 at 19:47
-
I thought it could natively handle floating-point image data. Let me see what I can find. --- EDIT: yes, it seems it can. Do you mean something else? – Mr.Wizard Oct 15 '11 at 19:49
-
@Mr.Wizard What is the minimal difference? I will make a separate question if you wish. – Alexey Popkov Oct 15 '11 at 19:56
-
Perhaps you are looking for `Image[RandomInteger[{0, 65535}, {50, 50, 3}], "Bit16", ImageSize -> 200]` i.e. 16-bit color? – Mr.Wizard Oct 15 '11 at 19:56
-
@Mr.Wizard I am interested in the minimal difference in RGB color values which *Mathematica* renders and exports as different colors. One example: `Rasterize[Graphics[{RGBColor[0, 0, 0], Disk[]}]] === Rasterize[Graphics[{RGBColor[0, 0, 1/256.4971], Disk[]}]]` gives `True` on my system. – Alexey Popkov Oct 15 '11 at 20:00
-
I am sorry, I misunderstood. I think the graphics in the FrontEnd are rendered in whatever your computer is set for, which as far as I know is still limited to 8-bit/channel for normal systems. I think this would make a good question to post. – Mr.Wizard Oct 15 '11 at 20:03
-
@Mr.Wizard I have created [separate question](http://stackoverflow.com/questions/7780327/what-is-the-minimal-difference-in-rgb-color-values-which-mathematica-renders-and) on it. – Alexey Popkov Oct 15 '11 at 20:08
I am not certain I understand your requirements, but this may be adequate.
The critical bits are PlotStyle -> None
and Background -> None
.
g = Plot3D[{x^2 + y^2, -x^2 - y^2}, {x, -2, 2}, {y, -2, 2},
RegionFunction -> Function[{x, y, z}, x^2 + y^2 <= 4],
BoxRatios -> Automatic, PlotStyle -> None, Axes -> None, Boxed -> False]
Export["wireframetest.png", g, Background -> None]
You could also export as GIF, but this doesn't actually show the background as transparent. You have to set the white background as the transparent color in a graphics editor. (This was tested on Mac OS X.)
-
`Export` to PNG [has no option `Background`](http://reference.wolfram.com/mathematica/ref/format/PNG.html) - this is why it does not work. See my answer for a solution. – Alexey Popkov Oct 15 '11 at 09:39
-
-
@Alexey, I just confirmed, it works as expected on my system. Without `Background -> None` the object is opaque; with it, it is transparent. – Mr.Wizard Oct 15 '11 at 12:38
-
I apologize. I read the answer carelessly and thought that the note "but this doesn't actually show the background as transparent" is related to both PNG and GIF export. I have not checked myself. At the same time, I do not know where the option `Background` is documented for `Export` and thought that it was a mistake to use it. I have deleted my answer. +1 for this catch. But where is it documented that `Background` should work for `Export`? – Alexey Popkov Oct 15 '11 at 14:25
-
1@Alexey, that's okay. For what it's worth, that was part of Verbeia's edit. I cannot recall if it is documented, but this was shown in a post on MathGroup, and I made a note of it. I believe the form that you posted was shown here (by you?) for a solution to other export requirements (GIF perhaps?), but I usually use the simpler one, and I prefer PNG because it supports full Alpha channel transparency. – Mr.Wizard Oct 15 '11 at 16:16
-
Thanks, I did not know that [GIF supports only fully transparent pixels but not semi-transparent](http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Palettes). You gave me the key: `Export` has separate option `"TransparentColor"` for specifying transparent color in the GIF palette. See new version of my answer. – Alexey Popkov Oct 15 '11 at 17:43