Skip to main content
Python

πŸ”₯ Turned JPGs into PDFs in Seconds

Have you ever stared at a folder full of JPGs, wondering why there isn't a simple one-click way to turn them into a nice, neat PDF?

G

gs_admin

Author & Reviewer

Published

Apr 05, 2025

Read Time

2 min read

article.txt
πŸ“°
Python

Have you ever stared at a folder full of JPGs, wondering why there isn't a simple one-click way to turn them into a nice, neat PDF?

Well, I had that moment. And instead of going down the usual "online converter full of ads" route, I called in my trusty coding sidekick β€” Python. Spoiler: it listened. And in just a few lines of code, my images transformed into a polished PDF file.

Here's how you can make your own JPG-to-PDF converter using Python in less than 5 minutes.

JPG to PDF with Python
JPG to PDF with Python

🧰 What You'll Need

Before we dive in, make sure you've got:

  • Python installed (you can download it from python.org)
  • The Pillow library (it's a fork of PIL, and it makes image handling a breeze)

To install Pillow, run this in your terminal:

bash
1
pip install pillow

πŸ§ͺ The Script That Talks to JPGs

Here's the full code to convert all .jpg images in a folder into one PDF file:

python
123456789101112131415161718192021222324252627
from PIL import Image
import os

def jpgs_to_pdf(output_pdf="output.pdf", directory="."):
    # Get all .jpg files in the directory
    jpg_files = [f for f in os.listdir(directory) if f.lower().endswith(".jpg")]
    jpg_files.sort()  # Optional: sort alphabetically

    if not jpg_files:
        print("No JPG files found.")
        return

    # Open images and convert to RGB
    image_list = []
    for file in jpg_files:
        img_path = os.path.join(directory, file)
        img = Image.open(img_path).convert("RGB")
        image_list.append(img)

    # Save as PDF
    first_image = image_list[0]
    remaining_images = image_list[1:]
    first_image.save(output_pdf, save_all=True, append_images=remaining_images)
    print(f"PDF created: {output_pdf}")

# Example usage
jpgs_to_pdf("combined_images.pdf", ".")

πŸ’‘ Pro tip: This also works for .jpeg files if you modify the filter:

python
1
if f.lower().endswith((".jpg", ".jpeg"))

πŸƒ How to Run It

  • Save the script as jpg_to_pdf.py.
  • Place it in the same folder as your JPG images β€” or adjust the directory path accordingly.
  • Open a terminal and run:
bash
1
python jpg_to_pdf.py

And voilΓ ! You'll find a shiny new combined_images.pdf in your folder, with all the images neatly packed in order.

🧠 Why Use Python Instead of Online Tools?

  • πŸ›‘ Privacy: Your images stay on your machine.
  • ⚑ Speed: It's lightning fast, even for large batches.
  • 🧩 Customizable: Add sorting, resizing, or watermarking with just a few more lines.

πŸš€ Wrap-Up

This little Python trick saves time, keeps things organized, and feels just so satisfying to run. Once you try it, you'll never go back to clunky online converters again.

So go ahead β€” talk to your JPGs. You'll be surprised how well they listen when Python speaks for you. πŸπŸ’¬πŸ“„

G

About the Author: gs_admin

A senior technical contributor specializing in architectural designs, software optimization, database structures, and developer education. Passionate about writing clean code and sharing engineering knowledge.