🐍 I Spoke to My JPGs Through Python. They Listened and Turned 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?
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.
🧰 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:
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:
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:
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:
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. 🐍💬📄
💬 Got questions? Drop a comment or reach out!