convert pdf to png with imagemagick

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;\

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!

24 Replies to “convert pdf to png with imagemagick”

  1. 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!

  2. 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!

  3. 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?



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


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


  6. 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!

  7. Guys,
    Lets try
    convert X.pdf -colorspace RGB -resample 72×72 -units PixelsPerInch -quality 90 -resize 500×500 -size 500×500 R.png

    it’s seems to run so as good as Robert script. ;)

  8. Hello guys;
    It may seem off-topic, but I am trying to get ImageMagick to work on Mac but I can’t do that unless I have the whole software installed. I need to distribute convert tool and I only want to package that with my software not all the stuff. Is there an easy way for me to get around that? i am not a Mac or Linux user, but I was able to do so for Windows by compiling the source.
    Any ideas? Your time is appreciated…

  9. Robfelty, Excellent article. Sounds like lots people are using your approach. I understand you don’t want to learn scripting on DOS, so for those who want to use your script on Windows, your script can be converted from bash to windows using biterscripting ( ) , as follows.

    var str list ; lf -n “*.pdf” > $list
    while ($list “”)
    var str file ; lex “1” $list > $file
    convert -density 600×600 -resize 800×560 -quality 90 $file ( { stex “]^.pdf^l” $file }+”.png” )

    Feel free to post this version of your script for windows, if you feel it’s appropriate.


  10. Hello, It’s my first encounter with ImageMagick, however I found it very promising, fast, elegant and straightforward. However my first steps aim to convert first PDF pages to png to be able to show them on website as thumbnails. The question is:

    why code:
    convert faktura.pdf -resample 72×72 -quality 90 faktura.png

    produces larger image (in pixels) but quite smaller file size than:
    convert faktura.pdf -resample 72×72 -quality 90 -resize 50% faktura.png

    I do not see de key.

  11. Just a thought– i found that ImageMagick produced the smallest images, with sharp, good-looking text, by converting to gif. Next largest is png, and biggest image files, for the same “sharpness” (not a technical term), is jpg. The png and jpg generally looked a lot more pixelated than the gif’s.

    Here’s my .bat for batch converting a whole directory of pdf’s:

    FOR %%a IN (*.pdf) DO convert -density 150x150 -scale 1000 %%a %%a.gif

    Thanks to Rob for showing me which option (density) is the key.

  12. Hi,
    for all of you, a similar script in windows batch format:

    @echo off

    for /f %%a in ('dir /b %~dp0\PDF-ORDNER\*.pdf') do call %~dp0\ImageMagick-6.7.3-10\convert.exe -density 600x600 -resize 800x560 -quality 90 %~dp0PDF-ORDNER\%%a %~dp0PNG-ORDNER\%%~na.png

  13. Brian–
    Or even better,
    That works for all extensions not just pdfs, and it doesn’t require a subprocess.

  14. Hi,

    Can anyone please help me, how I can get the response back after the images are converted from a given pdf?

Comments are closed.