PyMuPDF Pixmap tobytes() returns attribute error

Viewed 1298

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?

1 Answers

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