4

Can you help me to render a PDF page opened using PyPDF2 into a PIL image in Python 3?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
tairqammar
  • 151
  • 3
  • 10

1 Answers1

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