Batch Image to PDF Conversion with Python

In many work scenarios, you may need to handle multiple image files efficiently. Instead of opening each image individually, it's more productive to combine them into a single PDF document for easy previewing. Traditional tools like Adobe can be slow when processing large numbers of images, often displaying error messagse about quantity limits. Let's implement a Python solution for this requirement.

Implementation

1. Library Imports

First, we need to install and import the necessary libraries using pip:

pip install pillow
pip install tkinter

Then import the required modules:

from tkinter import Tk, Label, Button, Radiobutton, IntVar, messagebox
from tkinter import filedialog
from PIL import Image
import os

2. Core Conversion Logic

The following functions hendle the image-to-PDF conversion process:

def select_directory(self):
    """Open a dialog to select a folder containing images"""
    self.image_directory = filedialog.askdirectory()
    if not self.image_directory:
        messagebox.showinfo("Information", "No directory selected. Please choose a folder.")
    else:
        messagebox.showinfo("Information", f"Selected directory: {self.image_directory}")

def transform_images_to_pdf(self):
    """Convert images to PDF format based on user selection"""
    if not hasattr(self, 'image_directory') or not self.image_directory:
        messagebox.showinfo("Error", "Please select an image directory first.")
        return

    output_directory = os.path.join(self.image_directory, "pdf_output")
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)

    image_collection = []

    for image_file in os.listdir(self.image_directory):
        if image_file.lower().endswith((".png", ".jpg", ".jpeg")):
            image_path = os.path.join(self.image_directory, image_file)
            image = Image.open(image_path).convert('RGB')

            if self.composite_pdf_option.get() == 1:
                image_collection.append(image)
            else:
                pdf_filename = os.path.splitext(image_file)[0] + '.pdf'
                pdf_path = os.path.join(output_directory, pdf_filename)
                image.save(pdf_path)
                print(f"Converted {image_file} to PDF in {output_directory}.")

    if self.composite_pdf_option.get() == 1 and image_collection:
        merged_pdf_path = os.path.join(output_directory, "combined_images.pdf")
        image_collection[0].save(merged_pdf_path, save_all=True, append_images=image_collection[1:])
        print(f"All images merged into {merged_pdf_path}")

    messagebox.showinfo("Completion", "Conversion finished. Check the output folder.")


3. User Interface

The graphical interface allows users to select options and start the conversion:

def __init__(self, root):
    self.root = root
    self.root.title("Image to PDF Converter")

    self.composite_pdf_option = IntVar()
    self.composite_pdf_option.set(0)  # Default: separate PDFs

    Label(root, text="Convert images to PDF").pack()

    Button(root, text="Select Image Directory", command=self.select_directory).pack()

    Radiobutton(root, text="Combine into single PDF", variable=self.composite_pdf_option, value=1).pack()
    Radiobutton(root, text="Create separate PDF for each image", variable=self.composite_pdf_option, value=0).pack()

    Button(root, text="Start Conversion", command=self.transform_images_to_pdf).pack()

Tags: python PIL tkinter PDF conversion Image Processing

Posted on Thu, 23 Jul 2026 16:42:17 +0000 by Ju-Pao