1

I'm using below code to open a PDF file and convert into an image file as output. Now, i'm trying to figure it out how can I loop the next page and convert it as same output file. Any help is much appreciated!

# display image on the canvas
def openFile(self, _value=False):
    global fileImg, output


    path = os.path.dirname(ustr(self.filePath)) if self.filePath else '.'
    fileImg = QFileDialog.getOpenFileName(self, '%s - Choose file' % __appname__, path)

    # convert PDF to image file
    pdffile = fileImg
    doc = fitz.open(pdffile)
    page = doc.loadPage(0)
    pix = page.getPixmap(matrix=fitz.Matrix(100 / 72, 100 / 72))
    output = "output.png"
    pix.writePNG(output)
faizal_a
  • 93
  • 4
  • 15

2 Answers2

2

You can simply loop over the doc object to get the next pages.

doc = fitz.open(file_name)  # open document
for page in doc:  # iterate through the pages
    pix = page.getPixmap(...)  # render page to an image
    pix.writePNG("page-%i.png" % page.number)  # store image as a PNG

check the PyMuPDF documentation for more information.

Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
0

You can use minecart and use this snippet to split pdf into images

import minecart
from PIL import Image    
file =open('Yourdoc.pdf','rb')
    doc = minecart.Document(file)
    page=doc.iter_pages()
    pageref=[]
    for j,i in enumerate( page):
        im = i.images[0].as_pil()
        im.save(f"folderlocation/{j}.jpg")
user13966865
  • 448
  • 4
  • 13