0

I have attempted to use PyMuPDF to covert a PDF document to an image, so that I can use it in openCV. However I have an attribute error come up when I try to save the image and I'm not sure how to get around this?

import fitz
pdf = fitz.open('cornwall.pdf')
page = pdf.load_page(0)
pix = page.get_pixmap()
pix.writeImage("cornwall_output.png")

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

2 Answers2

0

use pil_save method instead https://pymupdf.readthedocs.io/en/latest/pixmap.html#Pixmap.pil_save

import fitz
pdf = fitz.open('cornwall.pdf')
page = pdf.load_page(0)
pix = page.get_pixmap()
pix.pil_save("cornwall_output.png") 
# optional arg in this method:
# optimize=True
smcrowley
  • 451
  • 3
  • 10
0

There is a standard way to save a PyMuPDF Pixmap: pix.save(). There is a handful of possible image formats available in this case: PNG, PSD (Adobe Photoshop), PS (Postscript) and the less popular PAM, PBM, PGM, PNM, PPM. Use pix.pil_save() instead only if you need more alternatives (e.g. JPEG) or special features offered by Pillow.

Jorj McKie
  • 2,062
  • 1
  • 13
  • 17