2

I'm following the documentation and using the latest PyMuPDF (1.18.13). However Pixmap.tobytes() isn't working for me:

zoom = 2    # zoom factor
mat = fitz.Matrix(zoom, zoom)
pix = page.getPixmap(matrix = mat)
stream = pix.tobytes(output="png")

AttributeError: 'Pixmap' object has no attribute 'tobytes'

Example of documentation: enter image description here

What might be the issue here?

2 Answers 2

4

I am PyMuPDF's maintainer. What is your configuration? I just tried your code on Windows and Linux each with v1.18.13 and it work.

Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fitz
>>> fitz.version
('1.18.13', '1.18.0', '20210505063222')
>>> doc=fitz.open("v110-changes.pdf")
>>> page=doc[0]
>>> pix=page.get_pixmap()
>>> b=pix.tobytes()
>>>

Windows:

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fitz
>>> fitz.version
('1.18.13', '1.18.0', '20210505063222')
>>> doc=fitz.open("v110-changes.pdf")
>>> page=doc[0]
>>> pix=page.get_pixmap()
>>> b = pix.tobytes()
>>>
2
  • I'm not sure what the issue is but I ended up using pix.pil_tobytes() which worked. I'm on ubuntu with python 3.8 as well. Thanks for your help anyway @JorjMckie
    – koopmac
    Commented May 31, 2021 at 22:59
  • The renaming effort in PyMuPDF lets the old names survive for some version as deprecated aliases. So you should also get away with pix.getPNGdata() or pix.detPNGData() at the time being - whatever went wrong at your place.
    – Jorj McKie
    Commented Jun 1, 2021 at 23:58
0

This solution can also be used along with Jorj's helpful answer:

for p in range(len(doc)):
    page = doc.load_page(p)
    pix = page.get_pixmap()
    img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

for a 200 dpi image, the processing time for "Image.frombytes()" is:

0:00:00.005696

for the same image using "pix.tobytes()" the processing time is:

0:00:00.105652

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.