10

I'm using GPL Ghostscript 9.07 (2013-02-14) on OS X (10.8.4) to convert many PDFs to PNGs.

It works fine except for one of the PDFs which turns into a PNG with jagged edges. In other words, Ghostscript turns off antialiasing for that particular PDF for some reason.

The PDF in question.

The output:

enter image description here

In other cases it works fine (sample: pdf -> png).

I use this command:

gs -dNOPAUSE -dBATCH -dPDFFitPage -sDEVICE=pngalpha -g200x150 -sOutputFile=01.png 01.pdf

Is it possible to force Ghostscript to use antialiasing for that PDF?

Any tips are appreciated.

Dae
  • 2,345
  • 2
  • 22
  • 34
  • So I solved my problem by converting the PDFs to SVG and using RSVG (rsvg-convert) for SVG to PNG conversion. – Dae Jul 11 '13 at 21:02

3 Answers3

13

This worked for me:

gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150  -sOutputFile=foo-%d.jpg foo.pdf

Source: ImageMagick convert pdf to jpeg has poor text quality after upgrading ImageMagick version to 6.7.8

The above would work for a JPG; for PNG, replace the -sDEVICE option with your choice, example: -sDEVICE=png16m

Source: http://ghostscript.com/doc/current/Devices.htm

Community
  • 1
  • 1
Meetai.com
  • 6,622
  • 3
  • 31
  • 38
  • The question was about converting from PDF to PNG, not JPEG. – Dae Jul 06 '14 at 15:26
  • 8
    It is worth noting that it is the options `-dTextAlphaBits=4` and `-dGraphicsAlphaBits=4` that do the work here. Other options may or may not be necessary depending on your situation. – Alex Trueman Feb 01 '18 at 15:17
  • `gs -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=output.png input.pdf` would suffice – vstepaniuk May 21 '20 at 18:48
2

You can try -dGraphicsAlphaBits= with values 1,2 or 4 which may or may not make a difference. It made some improvement for me, but its a small graphic at low resolution with an awkward curve, so not so much as might be expected.

Or you can use one of the anti-aliasing devices (eg tiffscaled) which are more flexible. There is no anti-aliased device for PNG output but it would be trivial to convert TIFF to PNG.

By the way, your PDF file specifically turns off anti-aliasing on the components:

8 0 obj
<</AntiAlias false/ColorSpace/DeviceCMYK/Coords[0.0 0.0 1.0 0.0]/Domain[0.0 1.0]/Extend[true true]/Function 10 0 R/ShadingType 2>>

You might like to try and see what happens if you change AntiAlias to true, though I doubt this will have an effect as I'm pretty sure the aniti-aliasing is applied to the internal rendering of the shading, not the edgses.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • 1) No changes when I set Antialias to true (had to uncompress and compress the file using pdftk to be able to edit it). 2) No changes for the jagged edges with -dGraphicsAlphaBits=4 3) Tiffscaled — YES, the produced image seems antialiased, but greyscale and the background is filled with white rather than transparent (I guess that's the limitation of TIFF). – Dae Jul 10 '13 at 11:53
  • Also PostScript, and PDF. The background is always assumed to be opaque white, unless you specifically draw something else in it. Its never 'transparent' in these imaging models. If you read the thread for the bug you opened in Bugzilla you'll see why other options aren't going to work, you need to use one of the 'scaled' devices. – KenS Jul 10 '13 at 11:56
1

You can try -dDOINTERPOLATE which uses a Mitchell filter function to scale the contributions for each output pixel

  • Unfortunately I don't see any changes, but thanks for the suggestion. – Dae Jul 10 '13 at 11:27
  • 1
    -dDOINTERPOLATE only affects images, it won't affect shading dictionaries, which is what this file uses. – KenS Jul 10 '13 at 11:56