koopmac
koopmac

Reputation: 966

PyMuPDF Pixmap tobytes() returns attribute error

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?

Upvotes: 2

Views: 2858

Answers (2)

Esraa Abdelmaksoud
Esraa Abdelmaksoud

Reputation: 1689

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

Upvotes: 0

Jorj McKie
Jorj McKie

Reputation: 3110

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()
>>>

Upvotes: 4

Related Questions