How to convert PDF to images in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 2

  • @hayatt143
    @hayatt143 2 ปีที่แล้ว

    How do we use poppler for PDF to HTML ?

  • @kawsarsajib8600
    @kawsarsajib8600 7 หลายเดือนก่อน

    Amazing video! Thank you very much. However, I have seen that the images are saved in the folder where the pdf is located. If I want to save it in another directory or folder I need to modify the code a little bit. The code is given below. My operating system is windows.
    import os
    from pdf2image import convert_from_path
    def convert_pdf(pdf_path, save_dir, res=400):
    pages = convert_from_path(pdf_path, dpi=res, output_folder=save_dir) # Specify the output_folder parameter
    name_with_extension = os.path.basename(pdf_path)
    name = os.path.splitext(name_with_extension)[0]
    for idx, page in enumerate(pages):
    page.save(os.path.join(save_dir, f'{name}_{idx}.png'), 'PNG', resolution=(1920, 1080))
    pdf_path = "C:\\Users\\Sajib\\Downloads\\50.pdf"
    save_dir = "C:\\Users\\Sajib\\Downloads\\output_images"
    convert_pdf(pdf_path, save_dir)
    print("done")
    def delete_ppm_files(directory):
    for filename in os.listdir(directory):
    if filename.endswith(".ppm"):
    os.remove(os.path.join(directory, filename))
    # Example usage:
    folder_path = "C:\\Users\\Sajib\\Downloads\\output_images"
    delete_ppm_files(folder_path)
    print("PPM files deleted successfully.")