How to Convert PDF to Image with Imagemagick from Command line

By | August 18, 2020

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 Imagemagick on Ubuntu

On ubuntu install it by typing the following in a terminal

$ sudo apt-get install imagemagick

For windows get the binaries from the website.

1. Convert 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 ... and so on for all pages 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.

Fix Permission Error

When using the above command you might get the following error:

convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
convert-im6.q16: no images defined `cc.jpg' @ error/convert.c/ConvertImageCommand/3258.

The above error is caused due to permission configuration. To fix the error edit the following file

$ sudo nano /etc/ImageMagick-6/policy.xml

And comment out the following lines:

<!-- disable ghostscript format types -->
  <!-- <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="PS2" />
  <policy domain="coder" rights="none" pattern="PS3" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="none" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" /> -->

Save the file, and re-run the command and it should work.

For more information on fixing the error, check this discussion on stackoverflow:
https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion

2. Creating PDF Thumbnail

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.

3. Clearer Text and Higher Resolution

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 demo.pdf[2] demo.jpg

4. 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.

5. 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.

Conclusion

Those were some very simple imagemagick examples to convert pdfs to images. These are useful when you are creating a web application that needs to create a image thumbnail or screenshot of a pdf.

If you have any feedback or questions, let us know in the comments below.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

2 Comments

How to Convert PDF to Image with Imagemagick from Command line
  1. Meg Tilton

    Thank you so much! Was getting terrible text quality on jpgs even with the quality set to 100. Setting density to 200 fixed the problem. All the other posts I found on this issue were too old to help, but this was exactly what was needed.

  2. billybobfrankwashington

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

    Thoughts?

Leave a Reply

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