6

When I use the following ghostscript command to generate jpg thumbnails from PDFs, the image quality is often very poor:

gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeggray -g465x600 -dUseCropBox -dPDFFitPage -sOutputFile=pdf_to_lowres.jpg test.pdf

By contrast, if I use ghostscript to generate a high-resolution png, and then use mogrify to convert the high-res png to a low-res jpg, I get pretty good results.

gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -g2550x3300 -dUseCropBox -dPDFFitPage -sOutputFile=pdf_to_highres.png test.pdf
mogrify -thumbnail 465x600 -format jpg -write pdf_to_highres_to_lowres.jpg pdf_to_highres.png

Is there any way to achieve good results while bypassing the intermediate pdf -> high-res png step? I need to do this for a large number of pdfs, so I'm trying to minimize the compute time.

Here are links to the images referenced above:

  1. test.pdf
  2. pdf_to_lowres.jpg
  3. pdf_to_highres.png
  4. pdf_to_highres_to_lowres.jpg
stevendaniels
  • 2,992
  • 1
  • 27
  • 31
Sharad Goel
  • 145
  • 1
  • 2
  • 6
  • Have a look at [this helpful answer](https://serverfault.com/a/797110/168205), it might fix your issue without using Ghostscript at all. It did for me. – likeitlikeit Sep 02 '17 at 10:10

2 Answers2

5

One option that seems to improve the output a lot: -dDOINTERPOLATE. Here's what I got by running the same command as you but with the -dDOINTERPOLATE option:

JPEG with -dDOINTERPOLATE

I'm not sure what interpolation method this uses but it seems pretty good, especially in comparison to the results without it.

P.S. Consider outputting PNG images (-sDEVICE=pnggray) instead of JPEG. For most PDF documents (which tend to have just a few solid colors) it's a more appropriate choice.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • +1 for "Consider outputting PNG images", there are no silver bullets in image compression, and jpeg is not a good choice for images with text or high contrast figures in general. – yms Dec 09 '13 at 00:01
  • If I could I'd vote 100 times this answer!! After days and days, -dDOINTERPOLATE has been the solution – David Feb 10 '17 at 18:16
3

Your PDF looks like it is just a wrapper around a jpeg already.

Try using the pdfimages program from xpdf to extract the actual image rather than rendering to a file.

David
  • 41
  • 3
  • 1
    That might work for this particular test file, but I'm hoping for a more robust solution that works even if the PDF is not wrapping a single image. – Sharad Goel Sep 21 '11 at 18:12