使用Python将PDF转换为一系列图像

Jae*_*ess 47 python pdf jpeg imagemagick python-imaging-library

我正在尝试使用Python将多页PDF转换为一系列JPEG.我可以使用可用的工具轻松地将PDF分割成单独的页面,但我无法找到任何可以将PDF转换为图像的内容.

PIL不起作用,因为它无法读取PDF.我发现的两个选项是通过shell使用GhostScript或ImageMagick.这对我来说不是一个可行的选择,因为这个程序需要跨平台,我不能确定这些程序中是否有任何一个可以安装和使用它们.

有没有可以做到这一点的Python库?

Ada*_*eld 20

ImageMagickPython绑定.

  • 注意详细说明哪些绑定可能有用? (17认同)
  • 我想开始,是的,您可以将 ImageMagick 库包含在您的项目中——只需确保您查看许可条款,以便将适当的内容放入自述文件中 (2认同)

小智 7

这里有什么用我使用python ghostscript模块(由'$ 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)
Run Code Online (Sandbox Code Playgroud)

我还在我的电脑上安装了Ghostscript 9.18,否则它可能不会起作用.

  • ghostscript似乎不支持python3 (2认同)