Converting multiple PDF files into JPG using ImageMagick

ImageMagick is an extremely powerful program, which can do amazing things even with very simple arguments. One of the things I have been using ImageMagick recently was to convert PDF files into image files (JPG, PNG, GIF, you name it), that is a task that many think that only can be achieved using some comercial (and expensive) tool. The basic usage is effortless:

convert input.pdf output.jpg

ImageMagick’s convert command will take the PDF as input and create a JPG file as output. Note that the type of the output file is determined by its extension, so if you wanted to create a PNG file, just use output.png. More advanced results include specifying the pixel density, quality, resize, scale, colorspace, alpha channel etc.. the list goes on beyond you could imagine. For a complete and detailed set of commands, check¬†http://www.imagemagick.org/script/command-line-options.php.

A more detailed example:

convert -alpha off -density 150 -quality 80 -resize 768 -unsharp 1.5 input.pdf output.jpg

The previous command will convert a PDF to JPG of quality 80, with 150 DPI and as last step, apply a 1.5 radius unsharp filter. Doing that with any programming language would require literally dozens of line of code.

One interesting option to note about is -alpha off, which prevents the JPG to be created with a black background. However, please note that may not be the case all the times, and you probably will have to adjust the arguments to fit your needs.

Batch processing

As a final example, the convert command requires either an input and an output names, but sometimes you want to the filename to be the same, just with a different extension (like Presentation_123.pdf => Presentation_123.jpg). This is very common when we have a set of pages in a given directory and want to convert them all. One easy way to accomplish that is by using the command mogrify instead of convert. mogrify works much like convert, but allows us to do some batch processing. For example:

mogrify -format jpg -alpha off -density 150 -quality 80 -resize 768 -unsharp 1.5 *.pdf

There are two different arguments: the first is the format we want to convert to, and the other is the format we are converting from.¬†The form is mogrify -format <output type desired> [other arguments] *.<input type>, so if you want to convert all JPG files to PNG without changing the filename (only the extension), you’d execute

mogrify -format png *.jpg

Easy as eating chocolate.

Leave a Reply

Your email address will not be published. Required fields are marked *


two * 7 =