Hassan Agmir Hassan Agmir

Image to PDF with Python

Hassan Agmir
Image to PDF with Python

Converting images to PDF is a common task in many fields, from document processing to digital archiving. Whether you want to combine scanned documents, create digital portfolios, or simply keep your images in an easily shareable format, Python provides powerful libraries to make the process simple and efficient. In this guide, we’ll explore why converting images to PDF is useful, the different approaches you can take with Python, and step-by-step examples of code you can use right away.

Why Convert Images to PDF?

Before diving into the technical part, let’s understand the importance of converting images to PDF:

  1. Professional Presentation – PDFs are widely used for business, legal, and academic purposes. Wrapping images into a single PDF makes them look polished and professional.
  2. Cross-Platform Compatibility – A PDF looks the same across all devices and operating systems, unlike images that may render differently depending on the viewer.
  3. Compression & Size Management – With proper settings, PDFs can be lighter than multiple high-resolution images.
  4. Archiving & Security – PDFs support encryption, watermarking, and password protection, making them ideal for sensitive documents.

Popular Python Libraries for Image to PDF Conversion

Python has a rich ecosystem of libraries that make image-to-PDF conversion simple:

  • Pillow (PIL fork): A powerful image processing library that allows you to open, manipulate, and save images. It also supports saving images directly as PDFs.
  • FPDF / ReportLab: Libraries designed specifically for PDF generation, allowing you to insert images into PDFs.
  • img2pdf: A dedicated library for lossless image-to-PDF conversion while maintaining quality.

Method 1: Using Pillow (PIL)

The Pillow library is one of the simplest ways to convert images to PDF.

Installation:

pip install pillow

Example Code:

from PIL import Image

# Open the image
image = Image.open("example.jpg")

# Convert to RGB (important for some formats like PNG)
if image.mode in ("RGBA", "P"):
    image = image.convert("RGB")

# Save as PDF
image.save("output.pdf")

This will create a single-page PDF containing your image.

Converting Multiple Images into One PDF:

from PIL import Image

images = ["img1.jpg", "img2.png", "img3.jpeg"]
image_list = []

for img in images:
    im = Image.open(img)
    if im.mode in ("RGBA", "P"):
        im = im.convert("RGB")
    image_list.append(im)

# First image is the cover, rest are appended
image_list[0].save("merged.pdf", save_all=True, append_images=image_list[1:])

Method 2: Using img2pdf

If you want high-quality and lossless conversion, img2pdf is a great choice. It directly encodes the image into a PDF without re-rendering, which helps preserve quality.

Installation:

pip install img2pdf

Example Code:

import img2pdf

# List of image files
images = ["img1.jpg", "img2.png", "img3.jpeg"]

with open("output.pdf", "wb") as f:
    f.write(img2pdf.convert(images))

This method is efficient and works well with multiple images.

Method 3: Using ReportLab

ReportLab is more flexible and allows you to customize page sizes, add text, and control formatting.

Installation:

pip install reportlab

Example Code:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

def image_to_pdf(image_path, output_pdf):
    c = canvas.Canvas(output_pdf, pagesize=letter)
    c.drawImage(image_path, 50, 500, width=500, height=300)
    c.save()

image_to_pdf("example.jpg", "reportlab_output.pdf")

This approach is useful if you want to create PDFs that mix images, text, and other design elements.

Which Method Should You Choose?

  • Quick and simple: Use Pillow.
  • High-quality and lossless conversion: Use img2pdf.
  • Custom layouts and design: Use ReportLab.

Real-World Use Cases

  1. Digitizing Receipts: Scan receipts as images and convert them into PDFs for tax and accounting purposes.
  2. Creating Photo Albums: Collect multiple images into a single PDF file for easy sharing.
  3. Archiving Documents: Save legal papers, contracts, or certificates as secure PDF documents.
  4. Educational Resources: Teachers can compile images of notes, diagrams, and assignments into PDF files.

Final Thoughts

Python makes image-to-PDF conversion seamless with its versatile libraries. Depending on your needs—whether it’s a quick conversion, preserving image quality, or building custom layouts—you have plenty of tools to choose from. With just a few lines of code, you can automate the entire process and save time while maintaining professionalism.

By leveraging Pillow, img2pdf, or ReportLab, you’ll be well-equipped to handle any image-to-PDF task efficiently.

Subscribe to my Newsletters

Stay updated with the latest programming tips, tricks, and IT insights! Join my community to receive exclusive content on coding best practices.

© Copyright 2025 by Hassan Agmir . Built with ❤ by Me