How to make a Python script to convert images to PDF

How to make a Python script to convert images to PDF

Convert Images to Compressed PDF using Python

In many scenarios, it becomes necessary to convert a collection of images into a single PDF file for easier sharing, archiving, or printing. However, large image files can result in bulky PDFs, making them difficult to handle or transmit. In this article, we will explore how to convert images into a compressed PDF using Python and the img2pdf library. This process reduces the file size of the resulting PDF while maintaining reasonable image quality.

Step 1: Installing the Required Library

pip install img2pdf

Step 2: Gathering the Images

First, we need to gather the images that we want to convert into a PDF. Ensure that the images are stored in a specific directory. In our example, we assume the images are located in the directory "C://Users//user//Desktop//pdf//imgs". You can adjust the path to match the location of your images.

Step 3: Converting Images to Compressed PDF

Now, let's dive into the Python code that performs the image-to-PDF conversion

import os
import img2pdf

path = "C://Users//user//Desktop//pdf//imgs"
dir_list = os.listdir(path)

webp_files = [x for x in dir_list if x.endswith(".webp")]

# Convert images to PDF with compression (adjust DPI)
pdf_bytes = img2pdf.convert([os.path.join(path, img) for img in webp_files], dpi=300)

# Save the PDF to a file
with open("compressed_images.pdf", "wb") as pdf_file:
    pdf_file.write(pdf_bytes)

print("Images converted to compressed PDF: compressed_images.pdf")

Let's break down the code

  • We import the necessary modules: os for working with the file system and img2pdf for the image-to-PDF conversion.

  • We define the path to the directory containing the images we want to convert.

  • Using os.listdir(path), we retrieve a list of all files in the specified directory.

  • We filter the list to include only files with the ".webp" extension, as indicated by x.endswith(".webp").

  • With the list of webp files obtained, we proceed to convert them into a compressed PDF. The img2pdf.convert() function takes a list of image file paths and an optional dpi parameter, which specifies the DPI (dots per inch) resolution of the images in the PDF. In our example, we set the DPI to 300, but you can adjust this value based on your requirements.

  • Finally, we save the PDF data to a file named "compressed_images.pdf" using a with the statement and the open() function in write binary mode ("wb").

Advance

import os
import img2pdf

path = "C://Users//user//Desktop//pdf//imgs" # Set Your Image Dir Path
dir_list = os.listdir(path)

print('''
░░░░░░░░▀████▀▄▄░░░░░░░░░░░░░░▄█
░░░░░░░░░░█▀░░░░▀▀▄▄▄▄▄░░░░▄▄▀▀█
░░▄░░░░░░░░█░░░░░░░░░░▀▀▀▀▄░░▄▀
░▄▀░▀▄░░░░░░▀▄░░░░░░░░░░░░░░▀▄▀
▄▀░░░░█░░░░░█▀░░░▄█▀▄░░░░░░▄█
▀▄░░░░░▀▄░░█░░░░░▀██▀░░░░░██▄█
░▀▄░░░░▄▀░█░░░▄██▄░░░▄░░▄░░▀▀░█
░░█░░▄▀░░█░░░░▀██▀░░░░▀▀░▀▀░░▄▀
░█░░░█░░█░░░░░░▄▄░░░░░░░░░░░▄▀
''')

user = input('''
1. Testing Text File.
2. PDF 
3. Exit
:- ''')


def dirTextFile(extension):
    imgEx = f".{extension}"
    webp_files = [x for x in dir_list if x.endswith(imgEx)]

    # Store the list of webp files in a text file
    with open("img_files.txt", "w") as file:
        for item in webp_files:
            file.write(item + "\n")

    print("files stored in img_files.txt")


def imgToPdf(dipp, setExt):
    print('Wait...')
    imgExten = f".{setExt}"
    webp_files = [x for x in dir_list if x.endswith(imgExten)]

    # Convert images to PDF with compression (adjust DPI)
    pdf_bytes = img2pdf.convert([os.path.join(path, img) for img in webp_files], dpi=dipp)

    # Save the PDF to a file
    with open("images.pdf", "wb") as pdf_file:
        pdf_file.write(pdf_bytes)

    print("Images converted to compressed PDF: images.pdf")


# main code start 
if user == '1':
    extension = input('Enter Image Extension ex(.png) - ')
    dirTextFile(extension)

elif user == '2':
    extension = input('Enter Image Extension ex(.png) - ')

    dip = input('''
1. Screen 72
2. Ebook 150
3. Prepress 300
:- ''')

    try:
        if dip == '1':
            setDip = 72
            imgToPdf(setDip, extension)

        elif dip == '2':
            setDip = 150
            imgToPdf(setDip, extension)

        elif dip == '3':
            setDip = 300
            imgToPdf(setDip, extension)

        else:
            print('Please Select Right Option...')

    except NameError:
        print(NameError)

# exit code 
elif user == '3':
    exit()
else:
    print('Please Select Right Option...')

Conclusion

In this article, we explored how to convert a collection of images to a compressed PDF file using Python and the img2pdf library. By adjusting the DPI parameter, we can control the resolution and resulting file size of the PDF. This technique allows for efficient storage, sharing, and transmission of multiple images in a single PDF document. Feel free to customize the code to suit your specific needs and explore additional options offered by the img2pdf library for further optimization or customization.

Did you find this article valuable?

Support Amit Gajare by becoming a sponsor. Any amount is appreciated!