Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PDF to image with high resolution

I'm trying to use the command line program convert to take a PDF into an image (JPEG or PNG). Here is one of the PDFs that I'm trying to convert.

I want the program to trim off the excess white-space and return a high enough quality image that the superscripts can be read with ease.

This is my current best attempt. As you can see, the trimming works fine, I just need to sharpen up the resolution quite a bit. This is the command I'm using:

convert -trim 24.pdf -resize 500% -quality 100 -sharpen 0x1.0 24-11.jpg 

I've tried to make the following conscious decisions:

  • resize it larger (has no effect on the resolution)
  • make the quality as high as possible
  • use the -sharpen (I've tried a range of values)

Any suggestions please on getting the resolution of the image in the final PNG/JPEG higher would be greatly appreciated!

like image 920
JBWhitmore Avatar asked Jul 07 '11 01:07

JBWhitmore


People also ask

How do I convert a PDF to an image without losing quality?

Open your PDF in Adobe Acrobat Pro and choose file. Export it to the new file format by going to the right pane and choosing “Export PDF” tool. Or, go to the menu and select “File” > “Export to” > “Image.” Choose image format type (e.g., JPG file, TIFF, etc.).


2 Answers

It appears that the following works:

convert           \    -verbose       \    -density 150   \    -trim          \     test.pdf      \    -quality 100   \    -flatten       \    -sharpen 0x1.0 \     24-18.jpg 

It results in the left image. Compare this to the result of my original command (the image on the right):

  

(To really see and appreciate the differences between the two, right-click on each and select "Open Image in New Tab...".)

Also keep the following facts in mind:

  • The worse, blurry image on the right has a file size of 1.941.702 Bytes (1.85 MByte). Its resolution is 3060x3960 pixels, using 16-bit RGB color space.
  • The better, sharp image on the left has a file size of 337.879 Bytes (330 kByte). Its resolution is 758x996 pixels, using 8-bit Gray color space.

So, no need to resize; add the -density flag. The density value 150 is weird -- trying a range of values results in a worse looking image in both directions!

like image 176
JBWhitmore Avatar answered Sep 22 '22 13:09

JBWhitmore


Personally I like this.

convert -density 300 -trim test.pdf -quality 100 test.jpg 

It's a little over twice the file size, but it looks better to me.

-density 300 sets the dpi that the PDF is rendered at.

-trim removes any edge pixels that are the same color as the corner pixels.

-quality 100 sets the JPEG compression quality to the highest quality.

Things like -sharpen don't work well with text because they undo things your font rendering system did to make it more legible.

If you actually want it blown up use resize here and possibly a larger dpi value of something like targetDPI * scalingFactor That will render the PDF at the resolution/size you intend.

Descriptions of the parameters on imagemagick.org are here

like image 30
majinnaibu Avatar answered Sep 22 '22 13:09

majinnaibu