python - 将 PDF 文件转换为多页图像

标签 python image pdf 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)

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

最佳答案

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),
    )

关于python - 将 PDF 文件转换为多页图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63661910/

相关文章:

python - 使用 python 3.5.0 在 Windows 8 上提取 Pdf 文本

python - 如何使用 Python 或 Bash 删除动态 DNS 记录?

python - 将 json 数据(未定义/困惑)转换为 DataFrame 的正确方法是什么?

javascript - 如何使用 JavaScript 将 base64 图像保存到用户的磁盘?

php - 在 php 中比较 2 个图像

html - 是否可以使用 Zend_Pdf 将 HTML 转换为 pdf?

python - 有什么方法可以像Python中的C++宏函数那样替换数字吗?

python - 如何在 Azure Devops 上的 ubuntu 镜像中为 matplotlib 使用 TkAgg 后端?

swift - 如何确定在 UIImage 而不是 UIImageView 上点击的 (x, y) 用户的位置?

java - PDFormX对象处理