繁体   English   中英

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

[英]I want to convert pdf to image using pdf2image in python on Mac OS X

我想在 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")

然后我得到空文件......我不明白发生了什么。 任何人的任何想法?

广

通过使用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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM