Merging Multiple Images into a Single PDF File Using Python

python @ Freshers.in

Often, we encounter scenarios where we need to consolidate multiple image files into a single PDF document. This process can streamline file organization, presentation preparation, and report generation. In this article, we’ll explore a Python-based solution to merge multiple images seamlessly into a PDF file.

Introduction

Python offers a plethora of libraries for image processing and PDF generation. Leveraging these libraries, we can automate the task of merging images into PDFs, saving time and effort. Here, we’ll utilize two primary libraries:

  • Pillow: A Python Imaging Library (PIL) fork, which provides easy-to-use methods for image manipulation.
  • ReportLab: A library for creating PDF documents programmatically.

Installation

Before diving into the code, ensure you have both Pillow and ReportLab installed. You can install them using pip, the Python package manager, with the following commands:

pip install Pillow
pip install reportlab

Step-by-Step Guide

Let’s break down the process of merging multiple images into a single PDF file using Python into simple steps:

Gather Image Files: Collect all the image files you want to merge into a single PDF and ensure they are in the same directory.

Import Libraries: Begin by importing the necessary libraries into your Python script:

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from PIL import Image
import os

Define Merge Function: Create a function to merge the images into a PDF file:

def merge_images_to_pdf(directory, output_pdf):
    image_files = [filename for filename in os.listdir(directory) if filename.endswith('.jpg')]
    image_files.sort()  # Sort the filenames

    c = canvas.Canvas(output_pdf, pagesize=letter)
    for filename in image_files:
        filepath = os.path.join(directory, filename)
        img = Image.open(filepath)
        img_width, img_height = img.size
        c.setPageSize((img_width, img_height))
        c.drawImage(filepath, 0, 0, width=img_width, height=img_height)
        c.showPage()
    c.save()

Specify Input and Output Paths: Set the paths for the directory containing the image files and the desired output PDF file:

directory_path = r'/path/to/directory'
output_pdf_path = r'/path/to/output.pdf'

Execute the Merge Function: Call the merge_images_to_pdf function with the specified directory and output PDF path:

merge_images_to_pdf(directory_path, output_pdf_path)

Run the Script: Save the Python script and execute it. Upon completion, you will find the merged PDF file containing all the images in the specified directory.

Author: user