【问题标题】:I want to convert pdf to image using pdf2image in python on Mac OS X我想在 Mac OS X 上的 python 中使用 pdf2image 将 pdf 转换为图像
【发布时间】:2020-05-26 03:13:21
【问题描述】:

我想在 Mac OS X 上使用 python 中的 pdf2image 将 pdf 转换为图像。

    from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)
# define pdf path
# convert pdf to image(1200dpi)
pdf_path = Path(".")
images = convert_from_path(str(pdf_path), 1200)
# save image files one by one
image_dir = Path(".")
for i, page in enumerate(pages):
    file_name = pdf_path.stem + "_{:02d}".format(i + 1) + ".jpeg"
    image_path = image_dir / file_name
    # save JPEG
    page.save(str(image_path), "JPEG")

然后我得到空文件... 我无法理解发生了什么。 任何人的想法??

【问题讨论】:

  • pages 没有在任何地方定义,而且,"."pdf_path 很奇怪。应该是my_file.pdf 之类的吗?
  • 真的。我忘了定义页面。

标签: python macos image pdf


【解决方案1】:

希罗

通过使用pdf2image库,可以像这样将pdf转换为image

from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500) // where 500 is dpi

以 jpeg 格式保存页面

for page in pages:
    page.save('out.jpg', 'JPEG')

要转换 PDF 的第一页,请检查此示例,

from pdf2image import convert_from_path
pages = convert_from_path('file.pdf', 500)
pages = convert_from_path('file.pdf', 500, single_file=True)
pages[0].save('file.jpg', 'JPEG')

【讨论】:

  • 如果它对你有帮助,那么你可以在未来接受这个答案,让任何人相信这个。
猜你喜欢
  • 1970-01-01
  • 2021-01-14
  • 2021-12-07
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 2018-04-03
  • 2020-06-27
  • 1970-01-01
相关资源
最近更新 更多