DEV Community

OneFileKit
OneFileKit

Posted on

JPG and PNG to PDF Without Re-encoding: img2pdf vs Pillow

I often need to turn a handful of scans or screenshots into a single PDF. The job sounds trivial, and in Python it's tempting to open the images with Pillow and call .save("out.pdf"). That works, and it also quietly re-encodes your pictures. For a photograph you may not notice. For a screenshot or a signed form, you will. Here's the difference between wrapping the original bytes and converting them, and the tool I actually use.

What "to PDF" should mean

A PDF page can hold a JPEG almost as-is: the same compressed bytes, with a small amount of PDF wrapping around them. If that's what you do, the image in the PDF is the image you started with. No new compression artefacts, no change in sharpness, and the file grows only by the size of the container.

The other approach is to decode the image and encode it again, often as a new JPEG, when the PDF is written. That's a conversion, not a wrap. You pay for it in quality, and sometimes in file size too.

img2pdf: wrap, don't convert

img2pdf is a small tool with one job: put raster images into a PDF without re-encoding PNG, JPEG or JPEG 2000. Other formats are stored with the same kind of lossless compression PNG uses.

pip install img2pdf
# Debian/Ubuntu also has a package: sudo apt install img2pdf

img2pdf photo.jpg receipt.png -o album.pdf
Enter fullscreen mode Exit fullscreen mode

Each image becomes a page, in the order you list them. The same thing from Python:

import img2pdf
from pathlib import Path

Path("album.pdf").write_bytes(
    img2pdf.convert("photo.jpg", "receipt.png")
)
Enter fullscreen mode Exit fullscreen mode

I tested this with a 36,875-byte JPEG. The PDF came out at 38,324 bytes — a little over a kilobyte of container, and that's it. pdfimages -list (from poppler-utils) reported the image as jpeg. Extracting it with pdfimages -j gave me a file with the same MD5 hash as the original photograph. The JPEG inside the PDF was the JPEG I started with.

For a sharp PNG (a fake invoice with hard-edged text), img2pdf stored the image losslessly (enc showed as image in pdfimages -list), and the PDF was only slightly larger than the PNG.

By default, if the file has no DPI of its own, img2pdf assumes 96 dpi and makes the page the same size as the image. There's no white border. When you need a standard paper size for printing or for a form, set it:

img2pdf --pagesize A4 --fit into photo.jpg -o a4.pdf
Enter fullscreen mode Exit fullscreen mode

--fit into scales the picture so it sits entirely on the page. letter works as well as A4.

What Pillow actually writes

Pillow can write a PDF directly:

from PIL import Image

Image.open("photo.jpg").save("out.pdf", "PDF")
Enter fullscreen mode Exit fullscreen mode

Several pages:

from PIL import Image

first = Image.open("photo.jpg")
rest = [Image.open("receipt.png").convert("RGB")]
first.save(
    "out.pdf",
    "PDF",
    save_all=True,
    append_images=rest,
)
Enter fullscreen mode Exit fullscreen mode

convert("RGB") is needed for a PNG that still has an alpha channel; PDF pages as Pillow writes them are RGB.

I saved the same JPEG through Pillow. pdfimages -list still said jpeg, but the extracted image was not the original: different hash, different size, reported at 72 dpi rather than 96. Pillow had decoded it and written a new JPEG.

The PNG result was more surprising. img2pdf had stored it losslessly at 10.2K. Pillow wrote a JPEG, and the image inside the PDF was 18.7K — larger and lossy. Fine edges in screenshots and text are exactly what JPEG is bad at, so this is the case I care about most.

Pillow is the right tool when you're already in a Pillow pipeline and you need to compose, resize or watermark before you write the PDF. It is not the right tool if your only goal is "these files, as they are, in a PDF".

A quick comparison

img2pdf Pillow Image.save(..., "PDF")
JPEG Original bytes, wrapped Re-encoded as a new JPEG
PNG Lossless Re-encoded as JPEG
Default page size Image size at 96 dpi (if no DPI is set) Image size at 72 dpi
Multi-page List the files, in order save_all=True and append_images

If you want to confirm what a tool did, pdfimages -list output.pdf is the first thing I run. Look at the enc column (jpeg vs image) and, if it matters, extract the image and compare it to the original.

When I don't write a script

For a folder of scans I want to repeat next month, I use img2pdf. When someone just needs a couple of photos turned into a PDF, I point them at a web tool rather than talking them through pip. I built free JPG to PDF and PNG to PDF converters on OneFileKit, with no sign-up. You upload the images, put them in order, and download a single PDF.

For anything scripted, img2pdf is still what I reach for first.

Wrapping up

  • "Image to PDF" can mean wrapping the original bytes, or decoding and encoding them again. Those are different jobs.
  • img2pdf keeps JPEG and PNG losslessly. I checked with a hash, not just a file size.
  • Pillow's PDF export re-encodes JPEGs and turns PNGs into JPEGs.
  • Use pdfimages -list to see what actually landed in the file.

If you've found a case where img2pdf can't wrap the original encoding, I'd like to hear about it in the comments.

Top comments (0)