convert pdf to png with imagemagick

March 11th, 2008

Imagemagick is a swiss-army knife of command-line image conversion, but can be a bit complicated to actually use. I have been making most of my figures with R lately, and printing them to pdfs, which I can include very easily into documents with pdflatex. I like pdf because it is scalable, fairly small file size (smaller than .eps), and portable. But today a colleague wanted to include a few of my figures in her own powerpoint presentation, and powerpoint only likes bitmaps. She was just going to take screenshots of the figures, but I quickly said, “no, I will just convert them to pngs”. She replied: “I don’t want you to go to a bunch of trouble.” “No trouble at all,” I replied. Then I quickly wrote a bash for loop to convert all the pdf figures into pngs. Then an hour later when I went to zip them up and e-mail them to her, I realized that they looked like crap. After a bit of searching online, I found the flags I was looking for, and eventually used:

for file in *.pdf; do \
echo $file;\
convert -density 600x600 -resize 800x560 -quality 90 $file `echo $file|cut -f1 -d'.'`.png;\
done

And now the code explained:
-density 600×600 says treat the pdf as 600×600 dpi resolution
-quality 90 says use the highest compression level for png (9) and no filtering (0)
-resize 800×560 gives the dimensions in pixels of the resulting png file

Happy ImageMagicking!

  • FriendFeed
  • Reddit
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Facebook
  • Fark
  • TwitThis
  • LinkedIn

10 Responses to “convert pdf to png with imagemagick”

  1. On March 11th, 2008 at 11:25 pm
    Daniel Reeves wrote:

    What you meant to say was “it’s an insane amount of trouble, but I love it”.

  2. On March 12th, 2008 at 8:21 am
    robfelty wrote:

    It really wasn’t that much trouble. If I had known the correct options to begin with, it would have only taken a minute or so (just waiting for imagemagick to work). Looking up the correct options probably took about 20 minutes. This is still far less time than it would have taken to open them up in Photoshop and export them by hand. And next time I want to do it, I will know exactly where to look for the correct options — my blog!

  3. On March 12th, 2008 at 9:12 am
    Memming wrote:

    Nice simple script :)

  4. On March 31st, 2008 at 8:19 am
    rich yumul wrote:

    Thanks for posting this article! I was tinkering with the options, trying to get nice PNG’s, wholly underwhelmed by the quality of the default settings of ImageMagick. Googling your article took a lot less time than I had already spent experimenting with the covert options. Thanks!

  5. On June 23rd, 2008 at 11:21 pm
    Ken wrote:

    Yes, Sir. Thank you, Sir.

  6. On July 10th, 2008 at 2:25 am
    Ed Hirschman wrote:

    Hi,

    I am trying to use your batch file to convert multiple pdf files to png under windows XP but cannot get it to run. Is the syntax correct for XP?

    Thanks,

    Ed

  7. On July 14th, 2008 at 3:40 pm
    robfelty wrote:

    @Ed,

    This is a bash script for unix/linux/mac operating systems. I know nothing about DOS batch files, nor do I wish to learn.

    Rob

  8. On September 7th, 2008 at 10:09 am
    Veg wrote:

    Thanks for bothering to blog this - that imagemagick incantation is *exactly* what I needed. You’re so right about the defaults being next to useless.

    Cheers

  9. On October 27th, 2008 at 12:04 am
    Rudy wrote:

    AWESOME! Me too: messed around with convert, then googled some crap until I found your excellent page. How to use convert to make PNGs out of PDFs!

  10. On November 18th, 2008 at 10:53 pm
    Aditya wrote:

    Thanks a ton. You saved me going through zillions of options of imagemagick.

Leave a Reply