简体   繁体   中英

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). 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. So far, the highest quality anti-aliased images that I can generate are using pdftoppm (for a given DPI resolution). I use the following command:

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

The equivalent command (or so I think) using ImageMagick was:

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. In fact there hardly seems to be any anti-aliasing in the ImageMagick-generated image. See the close-ups below:

pdftoppm image:

在此处输入图像描述

ImageMagick image:

在此处输入图像描述

So where this leaves me is that I am satisfied with the anti-aliasing that pdftoppm provides. However, ImageMagick seems to have more functionality in now converting the anti-aliased image such that the black background is transparent. I have applied the approaches detailed in this post using ImageMagick, but they compromise the quality of the anti-aliasing that was previously satisfactory.

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

EDIT :

I've managed to fix the issue of transparency based on some comments on the question I posted on the TeX stack exchange. 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?

The pdf file that I am converting can be found on this Dropbox link. Note that the font colour is white, and the background shows as white too (in my pdf viewer anyway), but is transparent. This is the converted PNG file.

You should use -density to increase the anti-aliasing of your PDF to PNG conversion. I note that your image is opaque white and the text is simply in the alpha channel.

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

在此处输入图像描述

If on Imagemagick 7, change convert to 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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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