1

I want to convert the pdf pages into png and tif images. I am giving the fmt = png / fmt = tif; But still getting the resultant image in JPEG format only.Please help me in getting the correct output.

      images =  convert_from_path(
                pdf_file,
                dpi=300,
                fmt="png",
                size=(1240, None),
                output_folder=folder_path,
            )
            for image in images:
                image.save('example.png','PNG')

1 Answers1

0

You can try something like this:

import PyPDF2
from wand.image import Image
import io

# Open the PDF file in read-binary mode
pdf_file = open('Autotorino_gcp.pdf', 'rb')

# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(pdf_file)

for page_num in range(len(pdf_reader.pages)):
    dst_pdf = PyPDF2.PdfWriter ()
    dst_pdf.add_page (pdf_reader.pages[page_num])

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file = pdf_bytes, resolution = 300)
    ## Choose one format for the output 
    img.convert("png") # ('tiff')
    img.save(filename = "your_file_name")
Alessandro Togni
  • 680
  • 1
  • 9
  • 24