51

I'm attempting to use Python to convert a multi-page PDF into a series of JPEGs. I can split the PDF up into individual pages easily enough with available tools, but I haven't been able to find anything that can covert PDFs to images.

PIL does not work, as it can't read PDFs. The two options I've found are using either GhostScript or ImageMagick through the shell. This is not a viable option for me, since this program needs to be cross-platform, and I can't be sure either of those programs will be available on the machines it will be installed and used on.

Are there any Python libraries out there that can do this?

Jaearess
  • 677
  • 1
  • 7
  • 10

5 Answers5

21

ImageMagick has Python bindings.

Ketul
  • 126
  • 6
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 2
    I'd like to kick in that yeah, you can just include the ImageMagick library with your project -- just make sure you review the license terms so you can put the appropriate stuff in your readme – Coderer Dec 18 '08 at 15:43
  • 25
    Care to elaborate on what bindings might be useful? – Ivo Flipse Feb 12 '13 at 12:49
9

Here's whats worked for me using the python ghostscript module (installed by '$ pip install ghostscript'):

import ghostscript

def pdf2jpeg(pdf_input_path, jpeg_output_path):
    args = ["pdf2jpeg", # actual value doesn't matter
            "-dNOPAUSE",
            "-sDEVICE=jpeg",
            "-r144",
            "-sOutputFile=" + jpeg_output_path,
            pdf_input_path]
    ghostscript.Ghostscript(*args)

I also installed Ghostscript 9.18 on my computer and it probably wouldn't have worked otherwise.

Idan Yacobi
  • 91
  • 1
  • 3
  • This seems to create the new image, and I can see the pdf thumbnail, but for some reason it doesn't close the file of the new jpeg created. I got this output from python `##### 246643328 c_void_p(246643328L)` any ideas? – Jed Jun 05 '17 at 22:28
  • can you help solve my issue on this case? https://stackoverflow.com/questions/44448552/python-ghostscript-not-closing-output-file – Jed Jun 15 '17 at 18:05
  • 3
    ghostscript does not seem to have support for python3 – unlockme Nov 15 '17 at 16:48
  • 1
    @Idan Yacobi, thank you! this is the best solution for my python27. – Mark K Aug 24 '18 at 02:19
4

You can't avoid the Ghostscript dependency. Even Imagemagick relies on Ghostscript for its PDF reading functions. The reason for this is the complexity of the PDF format: a PDF doesn't just contain bitmap information, but mostly vector shapes, transparencies etc. Furthermore it is quite complex to figure out which of these objects appear on which page.

So the correct rendering of a PDF Page is clearly out of scope for a pure Python library.

The good news is that Ghostscript is pre-installed on many windows and Linux systems, because it is also needed by all those PDF Printers (except Adobe Acrobat).

Franz
  • 1,324
  • 8
  • 2
1

If you're using linux some versions come with a command line utility called 'pdftopbm' out of the box. Check out netpbm

Jay
  • 13,803
  • 4
  • 42
  • 69
1

Perhaps relevant: http://www.swftools.org/gfx_tutorial.html

mattbasta
  • 13,492
  • 9
  • 47
  • 68