Tuesday, September 14, 2010

Converting PDF to image

My previous post on Love Plus on the Wall Street Journal raised a few eyebrows, and a few questions.

One of them was: how did you convert the PDF of the front page of the WSJ into a image?

Ans: ImageMagick. Short answer:



convert -density 500x500 -quality 100 C:\LovePlus\WSJ.pdf C:\LovePlus\WSJ.jpg



Explanation:

ImageMagick is a vector image manipulation program. It can do so much more than convert PDF files to images; however that is beyond the scope of this article.

Under Windows (the syntax is virtually the same under Linux, where I first found this program) one just has to run the file convert.exe that is in the installation directory of ImageMagick.

The first parameter is "-quality". Self explanatory. More information can be found here. The value 100 means 100% of the quality. Be warned it will produce a huge file. Reduce as appropriate per individual needs.

The second parameter is "-density". I have to admit ImageMagick's documention on this parameter is rather vague. On the other hand I'm not sure how to explain it any better without going on about dots-per-inch, what a pixel really means, and the PostScript specification. Suffice to say the bigger the value the more quality ImageMagick can process out of a pure vectored PDF file, as usual at the expense of processing power and image size.

The 3rd parameter is where the PDF file is. Unless you have set the $PATH variable appropriately, you will need to specify the full path where the original PDF file(s) is. In this case its in C:\LovePlus, however, modify appropriately.

The 4th parameter is the output file. Again, unless you have set the $PATH variable, ImageMagick will write the output image file to the program directory of ImageMagick. Under Windows XP there will be no issue, as by default the user is running under full Administrative rights (which includes write permissions to the Program Files directory). Under Vista and Windows 7 the write will fail, unless the command window you are currently running has full Administrative rights.

Which one should not be doing simply to run ImageMagick!

To prevent the output from getting "lost", the full path is always desirable.

ImageMagick can do so much more than this. This hardly scrapes the surface. However, it should be enough to do a simple task: Converting the elements of a PDF to individual images.

No comments: