Mathematica:PNG透明背景

发布于 2024-11-15 12:09:58 字数 2518 浏览 3 评论 0原文

这很可能是 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}}
]

这是我获得的屏幕截图:

Output

首先要注意的是最后两张图片尺寸错误。这在某种程度上来说还不错,因为我对 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 文件的屏幕显示。

Images

所有这些都达到了预期的分辨率:)。但是我的透明背景怎么了?我已经通过我的代码多次指定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:

Output

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.

Images

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}}]

Images

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-11-22 12:09:58

首先:导出时不要使用ImageResolution!这个有缺陷的选项在大多数情况下都是无用的,并且不会达到人们所期望的效果。它的工作原理类似于 ImageSize

相反,请使用 Magnify 命令和 StyleMagnification 选项。他们做了我们想要从 ImageResolution 得到的事情!

现在关于问题第一部分中显示的行为原因。是的,它发生了。是的,它依赖于机器。这是一个错误吗?是的,至少是一个很大的缺陷。完全出乎意料吗?至于我——不是。

问题的根源似乎是图形的渲染实际上是由前端完成的,而后者在将图形转换为光栅时使用 Cell 的默认设置。

In[1]:= Options[Cell, PageWidth]
Out[1]= {PageWidth -> WindowWidth}

让我们尝试看看

Table[Rasterize[Style[x^2 + y^2, 40], Background -> None, 
  ImageResolution -> r], {r, {300, 400, 500, 600}}]

笔记本窗口的不同运行时大小的输出如何:

在此处输入图像描述

可以看到输出对笔记本窗口大小的明显依赖性。

解决方法是将 PageWidth 直接传递给 Cell

Rasterize[
 Cell[BoxData@ToBoxes@Style[x^2 + y^2, 40], PageWidth -> Infinity], 
 ImageResolution -> 600]

在此处输入图像描述


PS 另外。我知道 ImageResolution 按预期工作的唯一情况是使用 "AllowRasterization"->True 导出 PDF:

First@ImportString[
  ExportString[Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}], "PDF", 
   "AllowRasterization" -> True, ImageResolution -> 200], "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 of ImageSize.

Instead, use Magnify command and Magnification option of Style. They do what we want to get from ImageResolution!

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.

In[1]:= Options[Cell, PageWidth]
Out[1]= {PageWidth -> WindowWidth}

Lets try to see how output of

Table[Rasterize[Style[x^2 + y^2, 40], Background -> None, 
  ImageResolution -> r], {r, {300, 400, 500, 600}}]

looks at different run-time sizes of the notebook window:

enter image description here

One can see the obvious dependence of the output on the notebook window size.

A workaround is to pass PageWidth directly to Cell:

Rasterize[
 Cell[BoxData@ToBoxes@Style[x^2 + y^2, 40], PageWidth -> Infinity], 
 ImageResolution -> 600]

enter image description here


P.S. In addition. The only case I know when ImageResolution works as expected is PDF export with "AllowRasterization"->True:

First@ImportString[
  ExportString[Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}], "PDF", 
   "AllowRasterization" -> True, ImageResolution -> 200], "PDF"]

But it does not allow to specify transparent background.

霓裳挽歌倾城醉 2024-11-22 12:09:58

为了追踪错误的程度,以下是我从 Windows XP

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}}]

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

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}}]

20: enter image description here

40: enter image description here

100: enter image description here

300: enter image description here

400: enter image description here

500: enter image description here

600: enter image description here


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文