Converting a PDF to a series of images with Python

前端 未结 5 2067
执念已碎
执念已碎 2020-11-30 00:18

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 have

相关标签:
5条回答
  • 2020-11-30 00:50

    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.

    0 讨论(0)
  • 2020-11-30 00:53

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

    0 讨论(0)
  • 2020-11-30 00:54

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

    0 讨论(0)
  • 2020-11-30 01:01

    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).

    0 讨论(0)
  • 2020-11-30 01:09

    ImageMagick has Python bindings.

    0 讨论(0)
提交回复
热议问题