Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

GraphicsMagick: from PDF to an Antialiased Image

25 Jan 2014

An operation that editors like GIMP are naturally good at. However, GraphicsMagick won't smooth curves or text by default. Here's a way to get the same effect.

Clearly, antialiasing or not, you do want to use GraphicsMagick if you need to convert a large number of PDF pages to raster images. It's not something you want to repeatedly process manually with a graphical use interface, unless you choose to venture yourself to write a script with GIMP's programming interface.

1 The -antialias option

While GraphicsMagick's convert command comes with an option called -antialias, it doesn't appear to do what you'd expect when converting a PDF to a raster image. Or in fact, it does, it's just that the results are less than convincing. The -antialias option is only here to disable antialiasing (by passing +antialias, specifically, with a +), because it appears to be enabled by default, even if it doesn't look like it.

2 Playing with density and geometry

The trick is to increase the resolution, as in density, and correspondingly decrease the geometry again. So if you increase the density by 4 and decrease the geometry by 4 too, you'll get back to an image having the original dimensions. But during this process, GraphicsMagick will have implicitly used antialiasing to smooth it all nicely.

GraphicsMagick comes with the -density option to set the resolution. The problem is that it expects an absolute value in dots per inch (dpi), meaning that in order to be able to increase the density by 4, you need to work out what the current screen density is in the first place. It's something which you can experimentally determine. Suppose you have a 595×842 image. If you apply gm convert -density 72 to it and it still results to a 595×842 image, you'll know that the current density if 72 dpi. Therefore, increasing the density by 4 means setting the -density option to 72×4=288 dpi.

All you need to do next is to have GraphicsMagick set the geometry to a ¼ of the resulting dimensions. And it's interesting to see that you can do so on the fly, in the same GraphicsMagick command, simply as an extra option:

gm convert -density 288 -geometry 25% in.pdf out.png

3 References