If you’ve ever needed to merge multiple JPG images into a single PDF (for scans, documents, or notes), you can do it easily with Python using the Pillow library.
This guide walks you through a simple script that does exactly that.
Make sure you have Python installed. Then install Pillow:
pip install pillow
Here’s a minimal working script where you define the images and output file directly:
from PIL import Image
import os
# === CONFIG ===
output_pdf = "output.pdf"
image_files = [
"img1.jpg",
"img2.jpg",
"img3.jpg",
]
# ==============
def jpgs_to_pdf(image_files, output_pdf):
images = []
for file in image_files:
if not os.path.exists(file):
print(f"File not found: {file}")
continue
try:
img = Image.open(file)
# Convert to RGB (required for PDF)
if img.mode != 'RGB':
img = img.convert('RGB')
images.append(img)
except Exception as e:
print(f"Error processing {file}: {e}")
if not images:
print("No valid images to process.")
return
first_image = images[0]
rest_images = images[1:]
first_image.save(
output_pdf,
save_all=True,
append_images=rest_images
)
print(f"PDF created successfully: {output_pdf}")
if __name__ == "__main__":
jpgs_to_pdf(image_files, output_pdf)
image_files: a list of JPG files in the order you want them in the PDFoutput_pdf: the name of the resulting fileJust run:
python script.py