Wednesday 5 October 2011

Converting images from PDF to PNG using ImageMagick

I usually prefer vector formats like PDF for my figures, but some times I need to make a PNG version. ImageMagick is my tool of choice to do this, since it can be called from the command line. My initial try was something like this:

convert figure.pdf figure.png

which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:

convert -density 600 figure.pdf figure.png

As a little side comment, I could mention that I first tried to do this because when I saved a figure from Inkscape as PNG, the resolution was very poor. The workaround was then to save as PDF and convert to PNG. However, afterwards I remembered that Inkscape also has a "export bitmap" option, which will allow you to set the density before saving to PNG. Oh well, the ImageMagick conversion may still come in useful some day...

4 comments:

gmorais said...

it just did. I couldn't export a really big file in illustrator, it used all my 8gb of memory and still couldn't handle it. With imagemagick worked just fine.

Anonymous said...

yes, was handy for me too. I like batch converting pdfs.

Perry said...

There is another java library for pdf known as Aspose.PDF for Java which can convert pdf to png and png to pdf format in java. You should try this library also.

DomFilk said...

I'm not a developer, i always use this free online pdf to image converter to convert pdf to png online.

Post a Comment