1

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:

enter image description here

ImageMagick image:

enter image description here

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!

P.s. 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.

niran90
  • 248
  • 1
  • 10
  • 1
    Please post your PDF so others can test with it. – fmw42 Jun 22 '22 at 14:57
  • @fmw42 Can you suggest how I can link to my PDF in my post? After having Googled a bit, it seems that I cannot attach a file in these posts. – niran90 Jun 22 '22 at 15:27
  • 1
    Google Drive, or Dropbox or similar. – Mark Setchell Jun 22 '22 at 15:27
  • `@niran90` You can zip the PDF and attach the zip file. – fmw42 Jun 22 '22 at 16:11
  • `@niran90` Your PDF file is a solid white image! It is not your binary "m" character. Is that what you thought you were attaching. I do not see any text in it. Please check your posted file. – fmw42 Jun 22 '22 at 16:14
  • @fmw42 I'd mentioned in the EDIT section of my post that the text is white, and while the background is in fact transparent, it shows as white in the pdf viewer. When converting the pdf to png using ImageMagick, the background will show up as transparent on the PNG image. – niran90 Jun 22 '22 at 16:45
  • @fmw42 The 'm' images above are close-ups of a larger section of text. I have added a DB link for the converted PNG image too. – niran90 Jun 22 '22 at 16:48
  • 1
    @KJ Erm...okay. Not sure what to make of your comment, or your first two for that matter. You have a very cryptic style of writing that I find incredibly difficult to decipher. – niran90 Jun 22 '22 at 18:06

1 Answers1

2

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

enter image description here

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

enter image description here

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

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thank you for your answer! It's a bit strange, but when I apply the first two commands above, I get the error "magick: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `test.png' @ warning/png.c/MagickPNGWarningHandler/1750". Is there something I need to change in the policy.xml file? – niran90 Jun 22 '22 at 19:15
  • It does work fine with the `-colorspace RGB` flag though. The version I'm using is ImageMagick 7.1.0-39 – niran90 Jun 22 '22 at 19:22
  • In any case, I am now getting the same anti-aliasing quality as I did with pdftoppm! So I'm going to mark this as the correct answer :) – niran90 Jun 22 '22 at 19:40
  • 1
    PNG does not like to put an sRGB profile with a black/white or grayscale image. Adding -colorspace sRGB should be fine to make the image 3 channel. You can also just add PNG24:output.png or PNG32:output.png if you want 24-bit color (no transparency) or 32-bit color (transparency) for your output. – fmw42 Jun 22 '22 at 22:27
  • 1
    Okay, great. Thank you! Could you also elaborate a bit one what you mean by ".. should use -density to increase the anti-aliasing..."? Doesn't -density denote the DPI resolution? – niran90 Jun 22 '22 at 22:58
  • 2
    The density is used to convert the vector PDF to raster. When a high density is used, it is like scanning at higher density. If the image is too large (dimensions or file size) you can use -resize to shrink the image back to more normal size and still get a high quality anti-aliasing. This is like super-sampling. – fmw42 Jun 22 '22 at 23:19
  • Ahh, okay. Thanks for clarifying this :) – niran90 Jun 23 '22 at 06:48