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

This entry was posted in linux, mac osx and tagged , , , , . Bookmark the permalink.

24 Responses to convert pdf to png with imagemagick

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

  2. robfelty says:

    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. Memming says:

    Nice simple script :)

  4. rich yumul says:

    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. Ken says:

    Yes, Sir. Thank you, Sir.

  6. Ed Hirschman says:

    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. robfelty says:

    @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. Veg says:

    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. Rudy says:

    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. Aditya says:

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

  11. mauricio says:

    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. ;)

  12. Kamal says:

    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…
    Kamal

  13. Dave says:

    Thanks for the tip. This is exactly what I needed to convert some vector PDF files into PNGs.

  14. Richard says:

    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 ( http://www.biterscripting.com ) , as follows.

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

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

    Richard

  15. m227 says:

    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.

  16. johny why says:

    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.

  17. Pingback: PDF:t kauniiksi kuviksi. | Markuksen www-sielunmaisema

  18. Pingback: gpsbabel and R « Eye of an environmental scientist

  19. jungehexe says:

    thanks. it saved me a good half an hour collecting the flags myself.

  20. Brian says:

    This:

    `echo $file|cut -f1 -d'.'`

    can be simplified to this:

    `basename $file .pdf`
  21. Manoj says:

    Good One!! Thanks for Sharing information!!

  22. Andi says:

    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
  23. Spencer says:

    Brian–
    Or even better,

    "${file/#.*/.png}"

    That works for all extensions not just pdfs, and it doesn’t require a subprocess.

  24. Nandu says:

    Hi,

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