Saturday, February 09, 2013

How to convert(1) foo.pdf to bar.png with high resolution

Short version:use convert -density 300x300

I'm an occasional (→non-expert) user of the ImageMagick's marvelous convert program. Today I wanted to convert a nice PDF file to a high-resolution (300dpi, say) PNG file for some editing.

But I was having trouble with it; saying
convert foo.pdf bar.png
resulted in a 72dpi image. What to do? I wondered. Perhaps the help would be useful?

collin@p3:~> convert --help|grep -i resol
  -units type          the units of image resolution
  -resample geometry   change the resolution of an image
collin@p3:~> convert --help|grep -i size
  -page geometry       size and location of an image canvas (setting)
  -pointsize value     font point size
  -size geometry       width and height of image
  -adaptive-resize geometry
                       adaptively resize image using 'mesh' interpolation
  -extent geometry     set the image size
  -geometry geometry   preferred size or location of the image
  -repage geometry     size and location of an image canvas
  -resize geometry     resize the image
collin@p3:~> convert --help|grep -i dots
collin@p3:~> convert --help|grep -i dpi
collin@p3:~> 
None of those looked promising. I didn't want to change the resolution of the image, or resize the image; I just wanted the pre-existing information to be used! Well, when all else fails, Read The Fine Manual.
convert(1)                                                          convert(1)



NAME
       convert  -  convert  between  image formats as well as resize an image,
       blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much
       more.

SYNOPSIS
       convert [input-options] input-file [output-options] output-file

OVERVIEW
       The  convert  program is a member of the ImageMagick(1) suite of tools.
       Use it to convert between image formats as well  as  resize  an  image,
       blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much
       more.

       For more information about the convert command, point your  browser  to
       file:///usr/share/doc/packages/ImageMagick/www/convert.html          or
       http://www.imagemagick.org/script/convert.php.

Ah-HA! The manual is actually online. So I went to the link and looked for "resolution" -- it pointed me at resample, which I knew I didn't want, but I clicked on it anyway, which got me to http://www.imagemagick.org/script/command-line-options.php#resample, where I did a search for "dpi" (dots per inch). That led me to this entry:
-density width
-density widthxheight
Set the horizontal and vertical resolution of an image for rendering to devices.
This option specifies the image resolution to store while encoding a raster image or the canvas resolution while rendering (reading) vector formats such as Postscript, PDF, WMF, and SVG into a raster image. Image resolution provides the unit of measure to apply when rendering to an output device or raster image. The default unit of measure is in dots per inch (DPI). The -units option may be used to select dots per centimeter instead.

The default resolution is 72 dots per inch, which is equivalent to one point per pixel (Macintosh and Postscript standard).
I see that the image is interpreted as 72x72 dpi during conversion; if I resample after that, the higher-resolution info is already gone. But this "density" thing says we'll read the original image at the specified resolution, and then convert to the output format desired. Following are three commands; in the image on the right are the results.
collin@p3:/tmp> convert foo.pdf bar.png
collin@p3:/tmp> convert foo.pdf -resample 300x300 bar3-resampled.png 
collin@p3:/tmp> convert -density 300x300 foo.pdf bar3.png
The first command is the naive command to convert foo.pdf to bar.png; the top image is a screenshot, magnified, of bar.png. You can see that the image is pretty ugly.

The second command is the result of resampling the interpreted-as-72dpi image. The information was lost when foo.pdf was read (i.e., it was read at 72dpi); we then tried to expand the image to 300dpi. The edges are smoothed, so it doesn't look quite as ugly as the first image—if you don't look too close that is. But still, a lot of information was lost.

The last command retains more information from the original, and in the bottom image you can see how much nicer it looks.

No comments: