将 PDF 文件转换为多页图像

Dav*_*los 3 python pdf image pymupdf

我正在尝试使用 PyMuPDF 将多页 PDF 文件转换为图像:

pdffile = "input.pdf"
doc = fitz.open(pdffile)
page = doc.loadPage()  # number of page
pix = page.getPixmap()
output = "output.tif"
pix.writePNG(output)
Run Code Online (Sandbox Code Playgroud)

但是我需要将 PDF 文件的所有页面转换为多页 tiff 中的单个图像,当我给页面参数一个页面范围时,它只需要一页,有人知道我该怎么做吗?

ZdP*_*ter 7

import fitz
from PIL import Image

input_pdf = "input.pdf"
output_name = "output.tif"
compression = 'zip'  # "zip", "lzw", "group4" - need binarized image...

zoom = 2 # to increase the resolution
mat = fitz.Matrix(zoom, zoom)

doc = fitz.open(input_pdf)
image_list = []
for page in doc:
    pix = page.getPixmap(matrix = mat)
    img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
    image_list.append(img)
    
if image_list:
    image_list[0].save(
        output_name,
        save_all=True,
        append_images=image_list[1:],
        compression=compression,
        dpi=(300, 300),
    )
Run Code Online (Sandbox Code Playgroud)