Can you help me to render a PDF page opened using PyPDF2 into a PIL image in Python 3?
Asked
Active
Viewed 2,551 times
4
-
See working solution here: https://gist.github.com/jrsmith3/9947838 – Rufat Nov 25 '21 at 06:51
-
pdf2image would be one option – Martin Thoma Jun 18 '23 at 12:30
1 Answers
3
I don't think PyPDF2 can render a pdf page. However, you can use PyMuPDF to do this. Here is the tutorial of PyMuPDF.
Here is an example of rendering with 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)

cges30901
- 470
- 3
- 10