Image of wire frame from 3Dgraph-can it be exported

青春壹個敷衍的年華 提交于 2019-12-11 06:24:39

问题


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.


回答1:


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]



回答2:


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.)



来源:https://stackoverflow.com/questions/7760143/image-of-wire-frame-from-3dgraph-can-it-be-exported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!