Converting multi-page PDF to PNG using ImageMagick

Quick tutorial on converting PDF files to PNG using ImageMagick in CentOS.

Foreplay: install ImageMagick

First, install ImageMagick using yum. As ImageMagick is not available, you need to activate the EPEL repository in your CentOS installation.

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.rpm

Edit the /etc/yum.repos.d/epel.repo and change enabled=0 to enabled=1 in the [epel] section. You can alternatively use yum with –enablerepo epel if you don’t want to enable epel by default.

Next, install ImageMagick:

yum install ImageMagick

With ImageMagick installed, go to the folder where you have your PDF file.


The Act itself

In this example we are converting a multipage PDF file named “flags.pdf”. This PDF has 13 pages, each page has a flag, so the aim is to generate one PNG per PDF page. The simple approach is just to convert it directly:

convert flags.pdf flag.png

This creates 14 files named flag-0.png to flag-13.png. The downside of this simple approach is that you have no control over the size, alpha channels or any other aspect of the output file, but it is always worth a try – think of this as the “i’m feeling lucky” approach. If the defaults are not good enough, here are the parameters you need to play with:

  • -verbose
  • -depth [i]: force the output color depth: 8, 24 or 32 for alpha transparency
  • -resize [%]: the default output is not anti-aliased, therefore ugly as s**t. This parameter will resize the original, use 50% for best results
  • -density [dpi]: as far as i can tell, the only mechanism available to control the target PNG resolution…

More options are available in the official documentation.

In my case, this was the option that worked best:

convert -verbose -depth 32 -density 100 -resize 50% flags.pdf flag.png

Posted

in

by

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from try {} except

Subscribe now to keep reading and get access to the full archive.

Continue reading