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开发者_如何学Go fit the wireframe to a structure.
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]
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.)
精彩评论