使用 GhostScript 将 PDF 转换为透明 PNG
我尝试使用 Ghostscript 来光栅化 PDF 文件,但没有成功 透明背景 具有透明背景的 PNG 文件。 我有 到处搜索尝试同样事情的其他人的问题 据我所知,没有发布的解决方案可以归结为 指定 -sDEVICE=pngalpha,已使用我的测试文件。 在此刻 我真的很感激更有经验的人可以提供的任何建议或提示 提供。
我的测试 PDF 位于此处: http://www.kolossus.com/files/test.pdf< /a>
问题可能出在这个文件上,但我对此表示怀疑。 据我所知 可以看出,它没有指定的背景,当我用 像 Photoshop 或 Illustrator 这样的透明度感知应用程序,果然如此 以透明背景显示。 然而,当用 像 Adobe Reader 这样的应用程序,文件会以白色背景呈现。 我相信这与渲染 PDF 的应用程序有更多关系 与 PDF 本身相比——Adobe Reader 等应用程序假设您想查看 打印文档的外观如何,因此始终显示白色 艺术品背后的画布——但我不能确定。
我使用的 gs 命令是:
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -sOutputFile=test.png test.pdf
这会生成一个在边界框之外具有透明像素的 PNG 文件中的图稿的像素,但图稿内的所有像素 边界框在白色背景下被光栅化。 这是个问题 对我来说,因为我的作品有阴影和抗锯齿边缘,需要 保留在最终输出中,并且不能仅使用后处理 图像魔术师。 我的 PNG 输出示例与上面的 pdf 位于同一位置,末尾带有 .png(stackoverflow 不允许我在帖子中包含多个 url)。
有趣的是,我看不到使用 -dBackgroundColor 标志的效果,甚至 如果我将其设置为非白色,例如 -dBackgroundColor=16#ff0000。 也许 我对该标志语法的理解是错误的。
另外有趣的是,我没有看到使用 -dTextAlphaBits=4 的效果 -dGraphicsAlphaBits=4 标志尝试启用子像素抗锯齿。 我会 也感谢有关如何启用子像素抗锯齿的任何建议, 尤其是在文字上。
最后,我在 Mac OS 10.5.7 上使用 GPL Ghostscript 8.64,并且渲染 我试图设置的工作流程是生成透明的 PNG 图像 PrinceXML 输出的 PDF。 我直接调用 Ghostscript 进行光栅化,而不是使用 ImageMagick,因为 ImageMagick 委托 Ghostscript 进行 PDF 光栅化,并且我应该能够通过直接调用 GS 更好地控制光栅化。
感谢您的帮助。
——乔恩·沃尔夫
I am attempting, unsuccessfully, to use Ghostscript to rasterize PDF files with a
transparent background to PNG files with a transparent background. I've
searched high and low for questions from others attempting the same thing
and none of the posted solutions, which as far as I can tell come down to
specifying -sDEVICE=pngalpha, have worked with my test files. At this point
I would really appreciate any advice or tips a more experienced hand could
provide.
My test PDF is located here: http://www.kolossus.com/files/test.pdf
It could be that the issue is with this file, but I doubt it. As far as I
can tell, it has no specified background, and when I open the file with a
transparency-aware app like Photoshop or Illustrator, sure enough it
displays with a transparent background. However, when opened with an
application like Adobe Reader the file is rendered with a white background.
I believe that this has more to do with the application rendering the PDF
than with the PDF itself -- apps like Adobe Reader assume you want to see
what a printed document will look like and therefore always show a white
canvas behind the artwork -- but I can't be sure.
The gs command I'm using is:
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -sOutputFile=test.png test.pdf
This produces a PNG that has transparent pixels outside of the bounding box
of the artwork in the file, but all pixels that are inside the artwork's
bounding box are rasterized against a white background. This is a problem
for me, as my artwork has drop shadows and antialiased edges that need to be
preserved in the final output, and can't just be postprocessed out with
ImageMagick. A sample of my PNG output is at the same location as the pdf above, with .png at the end (stackoverflow won't let me include more than one url in my post).
Interestingly, I see no effects from using the -dBackgroundColor flag, even
if I set it to something non-white like -dBackgroundColor=16#ff0000. Perhaps
my understanding of the syntax of this flag is wrong.
Also interestingly, I see no effects from using the -dTextAlphaBits=4
-dGraphicsAlphaBits=4 flags to try to enable subpixel antialiasing. I would
also appreciate any advice on how to enable subpixel antialiasing,
especially on text.
Finally, I'm using GPL Ghostscript 8.64 on Mac OS 10.5.7, and the rendering
workflow I'm trying to get set up is to generate transparent PNG images from
PDFs output by PrinceXML. I'm calling Ghostscript directly for the rasterization instead of using ImageMagick because ImageMagick delegates to Ghostscript for PDF rasterization and I should be able to control the rasterization better by calling GS directly.
Thanks for your help.
-Jon Wolfe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我与您分享使用
-dTextAlphaBits=4
和-dGraphicsAlphaBits=4
标志的经验。 它们似乎不适用于所有文本。 我想出的“修复”是将图像渲染为所需大小的 4 倍,然后缩小图像。 幸运的是,ghostscript 渲染千兆像素 PNG 文件没有任何问题。更新
Ghostscript(最高版本 9)在渲染像素数超过 2.500.000(即 10 mb 像素缓冲区)的透明 PNG 时似乎也遇到了重大问题。 透明背景突然变成白色。
深入研究 Ghostscript 的源代码,我发现当像素缓冲区大小超过 10 mb 时,它会切换到不同的内存分配方案。 更具体地说,图像是使用设备
image32
而不是pngalpha
渲染的。 鉴于pngalpha
驱动程序的实现方式,当 gs 决定不使用其pngalpha_fill_rectangle()
时,它的全部目的就消失了。幸运的是,有一个名为
-dMaxBitmap=N
的开关可以在运行时配置此参数。 这是在 1999-01-15 的一个完全不同的错误的解决方法中提到的,请参阅 http://pages.cs.wisc.edu/~ghost/doc/AFPL/5.50/relnotes/index.htm。添加
-dMaxBitmap=2147483647
为我解决了很多问题。 在 64 位系统上,该数字可能更高。真正的解决方法当然是重新设计
pngalpha
驱动程序,以便将背景颜色设置为0x7f000000
,无论实际代码路径如何,但大多数系统都有足够的内存为了使上述技巧发挥作用。I share your experience with the
-dTextAlphaBits=4
and-dGraphicsAlphaBits=4
flags. They appear not to work on all texts. The "fix" I came up with was to just render the image at 4 times the desired size, and then scale the image down. Luckily ghostscript has no problems rendering gigapixel PNG files.update
Ghostscript (up to version 9) also seems to enjoy major problems when rendering transparent PNG's with a pixel count above 2.500.000 (i.e. 10 mb of pixel buffer). The transparent background suddenly turns white.
Drilling down the source of ghostscript, I found that when the pixel buffer size exceeds 10 mb, it switches to a different memory allocation scheme. More specifically, the image is rendered using device
image32
instead ofpngalpha
. Given the way thepngalpha
driver is implemented, it's whole purpose vanishes when gs decides not to use itspngalpha_fill_rectangle()
.Luckily, there is a switch called
-dMaxBitmap=N
to configure this parameter at runtime. This is mentioned in a workaround for a totally different bug dating back to 1999-01-15, see http://pages.cs.wisc.edu/~ghost/doc/AFPL/5.50/relnotes/index.htm.Adding
-dMaxBitmap=2147483647
solved a lot of problems for me. On 64 bit systems, this number can be higher.A true fix would of course be to rework the
pngalpha
driver so that it sets the background color to0x7f000000
no matter the actual code path, but most systems have enough ram on board for the above trick to work.恐怕我不能告诉你 Ghostscript 能做什么,但我确实有一个建议。 尝试使用黑色和白色背景渲染 PDF。 两个图像中任何相同的像素显然都是不透明的(即 alpha == 1.0)。 不同的像素具有非零 alpha,可以通过从白色背景像素中减去黑色背景像素来计算。 给予或采取一定的精度,任何红色、绿色或蓝色分量的值都将是 alpha 值。
Afraid I can't tell you what ghostscript can do but I do have a suggestion. Try rendering your PDF with both a black and a white background. Any pixel that comes out the same in the two images was clearly meant to be opaque (i.e., alpha == 1.0). Pixels that are different have a non-zero alpha which can be computed by subtracting the black background pixel from the white background pixel. Give or take some precision, the value of any red, green or blue component will be the alpha value.
仅当在构建时选择了“transpar”选项时,Ghostscript 才能处理透明度。 假设您的 Ghostscript 版本正确,您可以在命令行上添加一个参数:(
您可以使用 [0..1] 范围内的不透明度变体)。
另外,您是否尝试过使用 ImageMagick 的
convert< 将 Ghostscript 创建的 .png(如果背景确实是白色而不是透明的)转换为透明背景/code> 或 GraphicMagick 的
gm Convert
命令? 下面是一个示例:顺便说一句,如果 Acrobat 或 AcroReader 将所有页面背景显示为白色:即使对于真正透明的背景,这也是默认设置。 您可以在应用程序的设置选项中更改它:IIRC 该设置位于“页面显示”选项内并命名为 s.th。 例如“显示透明度光栅”。 (如果我详细查找它,它可能不会有太大帮助 - Adobe 在每个新版本中都会不断改变所有选项。只要您自己四处看看,您就会发现它,因为您知道它就在那里。)< /em>
Ghostscript can handle transparency only if at build time the "transpar" option was selected. Assuming your Ghostscript is the right version, you can add a parameter on the commandline:
(You can use variants for the opacity in the range [0..1]).
Also, have you tried to convert your Ghostscript-created .png (in case that background indeed is white instead of transparent) to a transparent background using ImageMagick's
convert
or GraphicMagick'sgm convert
commands? Here is an example:BTW, in case Acrobat or AcroReader show all page backgrounds as white: this is the default setting even for really transparent backgrounds. You can change it in the application's setup options: IIRC the setting was inside the 'Page Display' options and named s.th. like 'Show transparency rasters'. (If I looked it up in detail, it possibly wouldn't help too much -- Adobe keeps shifting all options around in every new release. Just poke around there yourself, you'll find it now that you know it's there.)