I'm trying to convert a multipage PDF file to image with PyMuPDF:
pdffile = "input.pdf"
doc = fitz.open(pdffile)
page = doc.loadPage() # number of page
pix = page.getPixmap()
output = "output.tif"
pix.writePNG(output)
But I need to convert all the pages of the PDF file to a single image in multi-page tiff, when I give the page argument a page range, it just takes one page, does anyone know how I can do it?
import fitz
from PIL import Image
input_pdf = "input.pdf"
output_name = "output.tif"
compression = 'zip' # "zip", "lzw", "group4" - need binarized image...
zoom = 2 # to increase the resolution
mat = fitz.Matrix(zoom, zoom)
doc = fitz.open(input_pdf)
image_list = []
for page in doc:
pix = page.getPixmap(matrix = mat)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
image_list.append(img)
if image_list:
image_list[0].save(
output_name,
save_all=True,
append_images=image_list[1:],
compression=compression,
dpi=(300, 300),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With