Mathematica:PNG透明背景
这很可能是 Mathematica 8.0.1 中的错误,也可能是其他版本中的错误。让我们尝试以下操作:
Table[
Export[
"Res_" <> ToString[r] <> ".png", Rasterize[
Style[x^2 + y^2, 40],
Background -> None,
ImageResolution -> r
],
Background -> None],
{r, {20, 40, 100, 300, 400, 500, 600}}
]
这是我获得的屏幕截图:
首先要注意的是最后两张图片尺寸错误。这在某种程度上来说还不错,因为我对 300 或更高的分辨率感到满意。现在看看这个:
in = 72;
G3D = Graphics3D[AspectRatio -> 0.925, Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}}, AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12}, Boxed -> False,
BoxRatios -> {1, 1, 1}, LabelStyle -> Directive[Black],
ImagePadding -> All, ImageSize -> 5 in, PlotRange -> All,
PlotRangePadding -> None, TicksStyle -> Directive[10],
ViewPoint -> {2, -2, 2}, ViewVertical -> {0, 0, 1}, Background -> None
];
surf = Show[
Graphics3D[Sphere[{0, 0, 0}, 1], Background -> None,
AxesLabel -> {"x", "y", "z"}], Options[G3D]
];
fig = Show[surf, AxesStyle -> Directive[Opacity[0]],
Background -> None
];
我希望将无花果导出
作为具有透明背景的高分辨率的png文件。这是我对总是有问题的 Mathematica 的蹩脚尝试。
Table[
Export[
"Res_" <> ToString[r] <> ".png",
Rasterize[fig, ImageResolution -> r, Background -> None],
Background -> None
], {r, {20, 40, 100, 300, 400, 500}}
]
这是一些 png 文件的屏幕显示。
所有这些都达到了预期的分辨率:)。但是我的透明背景怎么了?我已经通过我的代码多次指定Background -> None
但这不起作用。我浏览了一下网络,发现了这个:
http://forums.wolfram .com/mathgroup/archive/2009/Feb/msg00943.html
让我们使用这个想法。
bgImage = Image[ConstantArray[{0, 0, 0, 0}, Reverse[ImageDimensions[fig]]],
ColorSpace -> "RGB"];
compImage = ImageCompose[bgImage, fig];
Table[Export["Res_" <> ToString[r] <> ".png",
Rasterize[compImage, ImageResolution -> r, Background -> None],
Background -> None], {r, {20, 40, 100, 300, 400, 500}}]
没有背景! :) 伟大的。但是我的图像尺寸发生了什么变化?分辨率不断增加,但图像尺寸开始减小。我真的已经被这个问题困扰太久了。我希望你们中的一个人能够阐明这个 Mathematica 错误,并找到一种破解方法来实现高分辨率的透明背景 PNG。如果您找到答案,请提及您正在使用的 Mathematica 版本。
This is most likely a bug in Mathematica 8.0.1 and maybe other versions too. Lets try the following:
Table[
Export[
"Res_" <> ToString[r] <> ".png", Rasterize[
Style[x^2 + y^2, 40],
Background -> None,
ImageResolution -> r
],
Background -> None],
{r, {20, 40, 100, 300, 400, 500, 600}}
]
This is a screen shot of what I obtain:
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:
in = 72;
G3D = Graphics3D[AspectRatio -> 0.925, Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}}, AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12}, Boxed -> False,
BoxRatios -> {1, 1, 1}, LabelStyle -> Directive[Black],
ImagePadding -> All, ImageSize -> 5 in, PlotRange -> All,
PlotRangePadding -> None, TicksStyle -> Directive[10],
ViewPoint -> {2, -2, 2}, ViewVertical -> {0, 0, 1}, Background -> None
];
surf = Show[
Graphics3D[Sphere[{0, 0, 0}, 1], Background -> None,
AxesLabel -> {"x", "y", "z"}], Options[G3D]
];
fig = Show[surf, AxesStyle -> Directive[Opacity[0]],
Background -> None
];
I wish to Export
fig as a png file with transparent background with a high resolution. Here goes my lame attempt with the always buggy Mathematica.
Table[
Export[
"Res_" <> ToString[r] <> ".png",
Rasterize[fig, ImageResolution -> r, Background -> None],
Background -> None
], {r, {20, 40, 100, 300, 400, 500}}
]
Here is a screenshow of a few png files.
All of them came out with the expected resolution :). But what happened to my transparent background? I have specified many times through my code Background -> None
and yet this doesn't want to work. I looked around the web and I found this:
http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00943.html
Lets use this idea.
bgImage = Image[ConstantArray[{0, 0, 0, 0}, Reverse[ImageDimensions[fig]]],
ColorSpace -> "RGB"];
compImage = ImageCompose[bgImage, fig];
Table[Export["Res_" <> ToString[r] <> ".png",
Rasterize[compImage, ImageResolution -> r, Background -> None],
Background -> None], {r, {20, 40, 100, 300, 400, 500}}]
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:导出时不要使用
ImageResolution
!这个有缺陷的选项在大多数情况下都是无用的,并且不会达到人们所期望的效果。它的工作原理类似于ImageSize
。相反,请使用
Magnify
命令和Style
的Magnification
选项。他们做了我们想要从ImageResolution
得到的事情!现在关于问题第一部分中显示的行为原因。是的,它发生了。是的,它依赖于机器。这是一个错误吗?是的,至少是一个很大的缺陷。完全出乎意料吗?至于我——不是。
问题的根源似乎是图形的渲染实际上是由前端完成的,而后者在将图形转换为光栅时使用
Cell
的默认设置。让我们尝试看看
笔记本窗口的不同运行时大小的输出如何:
可以看到输出对笔记本窗口大小的明显依赖性。
解决方法是将
PageWidth
直接传递给Cell
:PS 另外。我知道
ImageResolution
按预期工作的唯一情况是使用"AllowRasterization"->True
导出 PDF:但它不允许指定透明背景。
First of all: do not use
ImageResolution
when exporting! This buggy options is useless most of the time and does not do what one can expect. It works as an analog ofImageSize
.Instead, use
Magnify
command andMagnification
option ofStyle
. They do what we want to get fromImageResolution
!Now about the reason for the behavior showed in the first part of the question. Yes, it happens. And yes, it is machine-dependent. Is it a bug? Yes, at least a great imperfection. It it completely unexpected? As for me - not.
It seems that the source of the problem is that rendering of graphics is in really made by the FrontEnd and the latter uses default settings for
Cell
when converting graphics to raster.Lets try to see how output of
looks at different run-time sizes of the notebook window:
One can see the obvious dependence of the output on the notebook window size.
A workaround is to pass
PageWidth
directly toCell
:P.S. In addition. The only case I know when
ImageResolution
works as expected is PDF export with"AllowRasterization"->True
:But it does not allow to specify transparent background.
为了追踪错误的程度,以下是我从 Windows XP
20 上的 Mathematica 7.0.1 获取的图像:
40:
100:
300:
400:
500:
600:
我使用球体使用第一种方法获得的输出似乎与使用第二种方法获得的输出相匹配。即透明背景,但大小不同。如果需要,我将上传这些输出。
For the purpose of tracking down the extent of the error, here are the images I get from Mathematica 7.0.1 on Windows XP
20:
40:
100:
300:
400:
500:
600:
The output that I get for the first method with the spheres appears to match the output that you get with the second method. That is, a transparent background, but different sizes. I will upload these outputs if requested.