Python Forum

Full Version: more pdf questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
in my efforts to save server space, I have a "how to" problem.
Using Fitz (pyMuPdf), I can : open a pdf and find the page I need (page_nr)
file_handle = fitz.open(pdf_path)
page = file_handle[page_nr]
page_img = page.get_pixmap()
I can save this pixmap as png on disk. OK.
Then I open it with PIL (Image.open...) and show it on a Tkinter canvas.
Question:
Can I somehow skip the part where I save on disk and open it with PIL?
In other words: how do I get from "page.get_pixmap()", directly to an
image object which has width and height and can be placed on a Tkinter canvas.
The whole point being that I do not need this image on disk, it is already inside the pdf.
thx,
Paul

EDIT: I think it' easier than I thought:
img = Image.open(StringIO.StringIO(page_img))
<- no results
EDIT 2: to whom it may concern, you will find many suggestions to do this, tried many, in the end only one really worked:
pix = page.get_pixmap(matrix=mat)
data = pix.tobytes("ppm")
img = Image.open(io.BytesIO(data),mode='r',formats=None)
It would seem something has been altered in the pix.tobytes() area "pixmap-object-has-no-attribute-getimagedata"