簡體   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