Guide · 2 min read
Convert JPG to PDF Tool: Tested Python Code and CLI
By the PNG to PDF team ·
A convert JPG to PDF tool you write yourself is a few lines of Python with img2pdf, and it gives you something web converters do not: proof that the JPEG data went into the PDF unchanged. The code below was run on Windows with Python 3.11, img2pdf 0.6.3 and Pillow 13.0.0 on 2026-10-24. Install with pip install img2pdf pillow pikepdf.
Convert JPG to PDF tool: img2pdf code
img2pdf describes itself as lossless conversion of raster images to PDF, and JPEG data is embedded directly. One call turns a list into a multi-page PDF:
import glob
import img2pdf
files = sorted(glob.glob("photos/*.jpg"))
with open("photos.pdf", "wb") as f:
f.write(img2pdf.convert(files))
The command line does the same: img2pdf 01.jpg 02.jpg -o out.pdf. In the test, the Windows launcher img2pdf.exe, given three JPG files, produced a three-page PDF. For page size and margins, the layout functions in create PDF from images apply to this call.
Prove the JPEG was kept
A claim of lossless conversion is easy to test. This check lists the image filter and pixel size of every page with pikepdf:
import pikepdf
with pikepdf.open("photos.pdf") as pdf:
for number, page in enumerate(pdf.pages, 1):
for name, image in page.Resources.XObject.items():
print(number, image.Filter, int(image.Width), int(image.Height))
For three test photos the output was /DCTDecode with sizes 1200 by 800, 800 by 1200 and 1000 by 1000, which means the JPEG filter and the original pixel sizes. A Flate filter or a different size would mean the image had been processed.
Convert JPG to PDF with Pillow when you need edits
Pillow decodes the picture, so it is the choice when you want to resize, crop or fix rotation first. The PDF writer chooses the encoding from the image mode and uses JPEG for RGB, so the pixels are encoded again.
from PIL import Image, ImageOps
img = ImageOps.exif_transpose(Image.open("photo.jpg")).convert("RGB")
img.save("photo.pdf", resolution=150.0)
exif_transpose applies the photo’s rotation tag; without it, Pillow left a tagged photo sideways in the test. The resolution setting, with the pixel count, sets the page size. For a folder of files with correct sorting, see convert multiple JPG to PDF. PNG files get the same treatment in convert PNG to PDF free, and if you are wary of downloading programs, image to PDF converter free download explains how to verify them.
FAQ
How do I convert JPG to PDF in Python?
Use img2pdf: img2pdf.convert(files) returns the PDF bytes for a list of JPG files, which you write to a file.
Does img2pdf recompress JPG files?
It embeds JPEG data directly, per its description, and the check above showed the JPEG filter and the original pixel sizes.
When should I use Pillow instead?
When the image needs editing before it goes into the PDF. Pillow re-encodes RGB images as JPEG.