如何在python中将PyPDF.PageObject页面呈现为PIL图像?

时间:2018-12-09 19:03:04

标签: python-imaging-library pypdf2

您能帮我将使用PyPDF2打开的pdf页面呈现为python3中的PIL图像吗?谢谢!!!

1 个答案:

答案 0 :(得分:1)

我认为PyPDF2无法呈现pdf页面。但是,您可以使用PyMuPDF来执行此操作。这是PyMuPDF的tutorial

以下是使用PyMuPDF渲染的示例:

import fitz
from PIL import Image

filename = "test.pdf"  # name of pdf file you want to render
n = 0  # n is the page number

#render with PyMuPDF
doc = fitz.open(filename)
page = doc.loadPage(n)
pix = page.getPixmap()

#convert to a PIL image
img = Image.frombytes("RGBA", [pix.width, pix.height], pix.samples)