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?

koopmac
  • 936
  • 10
  • 27

2 Answers2

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()
>>>
Jorj McKie
  • 2,062
  • 1
  • 13
  • 17
  • 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 May 31 '21 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 Jun 01 '21 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
Esraa Abdelmaksoud
  • 1,307
  • 12
  • 25