Convert pdf to image with imagemagick from commandline

Imagemagick

Imagemagick is a commandline program/utility that can be used to perform a variety of image manipulations. It supports most image formats. It also has programming apis for various languages like php, python etc making it easy to use as a library.

Install

On ubuntu install it by typing the following in a terminal

$ sudo apt-get install imagemagick

For windows get the binaries from the website.

Pdf to image

Web applications dealing with pdfs sometimes need to create a image or thumbnail of the uploaded pdf. Over here we are going to use imagemagick to convert pdfs to images.

The simplest command to do the task is ...

$ convert demo.pdf demo.jpg

The above command shall generate the jpg format image from the pdf file. If the pdf file has multiple pages then imagemagick shall create multiple image files named as demo-1.jpg, demo-2.jpg ... for as many pages as there are in the pdf file.

To convert only a particular page from the pdf file use the following command

$ convert demo.pdf[2] demo.jpg

The 2 in the bracket indicates the index of the page. 0 means the first page and then increment 1 for each page.

Creating thumbnails

To create thumbnail, the image just needs to be scaled down using either of the 'scale', 'thumbnail' or 'resize' option. Here is a quick example.

$ convert -thumbnail x300 demo.pdf[2] demo.jpg

The above will create a thumbnail image of height 300px and correct width according to aspect ratio of the original pdf.

If the pdf has transparency then scaling might result into an image where all white areas turn into black. To fix this the 'flatten' option can be used as follows

$ convert -thumbnail x300 demo.pdf[2] -flatten demo.jpg

The above command shall put a white background in the transparent areas.

Clearer text

Using the above command you might notice that the text in the resulting image is not clear or sharp. This can be fixed using the 'density' option. Use a value of around 175 and the text should become clearer than before. Experiment with the value till the desired level of sharpness is achieved.

$ convert -density 200 -scale x800 demo.pdf[2] demo.jpg

Create gif animation of all pages

Imagemagick can even create a gif animation of all the pages of the pdf. Its as simple as the following command







$ convert -thumbnail x300 -delay 100 demo.pdf demo.gif

The delay parameter defines the delay of animation.

Quality/compression for jpg

The quality or compression level of jpg images can be specified using the 'quality' option

$ convert demo.pdf[0] -scale x800 -quality 75  -flatten demo75.jpg

Higher quality image would have lesser compression and larger file size as a result.

Last Updated On : 14th November 2012

Subscribe to get updates delivered to your inbox

1 Comment + Add Comment

  • I have some PDFs – and the text converts to jpg fine – but the background (or other images) are not coming through.

    Thoughts?

Leave a comment