简体   繁体   English

使用抗锯齿和透明背景将 PDF 转换为 PNG

[英]Converting a PDF to PNG with anti-aliasing and transparent background

I am trying to convert a LaTeX-generated PDF file to a PNG file with anti-aliasing and a transparent background (white text on a black background).我正在尝试将 LaTeX 生成的 PDF 文件转换为具有抗锯齿和透明背景(黑色背景上的白色文本)的 PNG 文件。 After having read the answer to this post and one of the comments to the answer, I compared the convert function of ImageMagick against pdftoppm.在阅读了这篇文章的答案和对答案的评论之一之后,我将 ImageMagick 的convert函数与 pdftoppm 进行了比较。 So far, the highest quality anti-aliased images that I can generate are using pdftoppm (for a given DPI resolution).到目前为止,我可以生成的最高质量的抗锯齿图像是使用 pdftoppm(对于给定的 DPI 分辨率)。 I use the following command:我使用以下命令:

pdftoppm -png -r 2000 text.pdf > text.png

The equivalent command (or so I think) using ImageMagick was:使用 ImageMagick 的等效命令(或者我认为)是:

convert +antialias -interpolate Nearest -density 2000 text.pdf -quality 90 -colorspace RGB text.png

However, I did not get as good-quality anti-aliasing using ImageMagick as I did with pdftoppm.但是,我使用 ImageMagick 并没有像使用 pdftoppm 那样获得高质量的抗锯齿。 In fact there hardly seems to be any anti-aliasing in the ImageMagick-generated image.事实上,ImageMagick 生成的图像中似乎几乎没有任何抗锯齿功能。 See the close-ups below:请看下面的特写:

pdftoppm image: pdftoppm 图像:

在此处输入图像描述

ImageMagick image: ImageMagick 图像:

在此处输入图像描述

So where this leaves me is that I am satisfied with the anti-aliasing that pdftoppm provides.所以这让我很满意 pdftoppm 提供的抗锯齿。 However, ImageMagick seems to have more functionality in now converting the anti-aliased image such that the black background is transparent.但是,ImageMagick 现在似乎在转换抗锯齿图像方面具有更多功能,使得黑色背景是透明的。 I have applied the approaches detailed in this post using ImageMagick, but they compromise the quality of the anti-aliasing that was previously satisfactory.我使用 ImageMagick 应用了这篇文章中详述的方法,但它们损害了之前令人满意的抗锯齿质量。

Can anyone advise me on how to solve the issue of obtaining a transparent background (which will always be black in color) while not affecting the anti-aliasing quality?谁能告诉我如何解决获得透明背景(始终为黑色)而不影响抗锯齿质量的问题? Additionally, if the ImageMagick command that I used above was sub-optimal for generating a high-quality anti-aliased image, is there a way that I can achieve both anti-aliasing as well as background transparency by using ImageMagick alone?此外,如果我上面使用的 ImageMagick 命令对于生成高质量的抗锯齿图像不是最佳的,有没有一种方法可以通过单独使用 ImageMagick 来实现抗锯齿和背景透明度? Any form of advice/tips would be much appreciated!任何形式的建议/提示将不胜感激!

Ps Since this question is partially LaTeX-related (I use LuaLaTeX to compile the PDF), I have posted a related question here regarding whether there is a much more straightforward way of directly generating the PDF file with a transparent background. Ps 由于这个问题部分与 LaTeX 相关(我使用 LuaLaTeX 编译 PDF),我在这里发布了一个相关问题,关于是否有更直接的方法可以直接生成具有透明背景的 PDF 文件。

EDIT :编辑

I've managed to fix the issue of transparency based on some comments on the question I posted on the TeX stack exchange.根据我在 TeX 堆栈交换上发布的问题的一些评论,我设法解决了透明度问题。 Now it's just about how I can improve the quality of anti-aliasing.现在只是关于如何提高抗锯齿的质量。 Is there a way that I can achieve the same quality anti-aliasing that I get from pdftoppm?有没有办法可以实现与从 pdftoppm 获得的相同质量的抗锯齿?

The pdf file that I am converting can be found on this Dropbox link.我正在转换的 pdf 文件可以在这个Dropbox 链接上找到。 Note that the font colour is white, and the background shows as white too (in my pdf viewer anyway), but is transparent.请注意,字体颜色为白色,背景也显示为白色(无论如何在我的 pdf 查看器中),但是是透明的。 This is the converted PNG file.是转换后的PNG文件。

You should use -density to increase the anti-aliasing of your PDF to PNG conversion.您应该使用 -density 来增加 PDF 到 PNG 转换的抗锯齿。 I note that your image is opaque white and the text is simply in the alpha channel.我注意到您的图像是不透明的白色,文本只是在 Alpha 通道中。

convert -density 600 text.pdf -alpha extract x.png

在此处输入图像描述

If on Imagemagick 7, change convert to magick.如果在 Imagemagick 7 上,请将 convert 更改为 magick。

If you want to keep the transparency and keep your text white, then如果要保持透明度并保持文本为白色,则

convert -density 600 text.pdf y.png

在此处输入图像描述

The image is above, but will look completely white and blend with the white background.图像在上面,但看起来完全是白色的,并与白色背景融为一体。 So you will need to download it.所以你需要下载它。

If you want black text on transparency, then如果你想要透明度上的黑色文本,那么

convert -density 600 text.pdf -alpha extract -alpha copy -channel rgb -negate +channel z.png

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM