
ImageMagick is an excellent open source set of software tools that helps with converting, editing, displaying and composing image files. Almost all programming languages have extensions or libraries to interact with the ImageMagick API, although you could also use it via command line.
Ghostscript is a set of tools that can interpret PostScript page description language and PDF files too, to render or rasterize them.
Using these tools, we can actually split apart a Portable Document Format (PDF) file into different images, 1 image per page.
Installation
On Mac, using homebrew its easy to install –
[bash]
$ brew install imagemagick
$ brew install ghostscript
[/bash]
On Ubuntu, use APT –
[bash]
$ sudo apt-get install imagemagick
$ sudo apt-get install ghostscript
[/bash]
Conversion
Using the convert tool which helps in conversion between various image formats as well as resize, crop, blur, etc. images, here’s how you’d convert a pdf file to an image –
[bash]
$ convert file.pdf image.png
[/bash]
Note: If you try the command without installing ghostscript then you may get an error like this (as ImageMagick calls Ghostscript to do the work):
[bash]
$ convert file.pdf image.png
convert: Postscript delegate failed `file.pdf’: No such file or directory @ error/pdf.c/ReadPDFImage/678.
convert: no images defined `image.png’ @ error/convert.c/ConvertImageCommand/3078.
[/bash]
Specifying Page Number
As you’d have noticed, the command converts all pages of pdf to individual images with names like image-[\d].png. What if we want to convert the first or lets say the third page ? Thats simple! Just append [page_number] to the pdf file name:
[bash]
$ convert file.pdf[0] image.png # convert the first page
$ convert file.pdf[2] image.png # convert the third page
[/bash]
Specifying Page Range
If you’d like to transform a range of pdf pages to images then the command slightly changes to something like this:
[bash]
$ convert file.pdf[5-10] images/image.png
$ ls images/
image-10.png image-6.png image-8.png
image-5.png image-7.png image-9.png
[/bash]
Converting multiple pages or ranges are also possible like this:
[bash]
$ convert file.pdf[5-10,15-22,35] images/image.png
$ ls images/
image-10.png image-17.png image-20.png image-35.png image-7.png
image-15.png image-18.png image-21.png image-5.png image-8.png
image-16.png image-19.png image-22.png image-6.png image-9.png
[/bash]
Hope this quick tip helps you with extracting a pdf into its respective pages as images.