Registry / serialization / img2pdf

img2pdf

JSON →
library0.6.3pypypi✓ verified 29d ago

img2pdf is a Python library designed for lossless conversion of raster images to PDF. It excels at embedding JPEG and JPEG2000 files without re-encoding, preserving original quality and minimizing file size, treating PDF primarily as a container format. For other image formats, it uses lossless zip compression. The current version is 0.6.3, and the library maintains an active release cadence with regular updates.

pip install img2pdf
INSTALL
IMPORT
SIG · IMG2PDF
I
img2pdf
serializationpythonv0.6.3
Install
3.7s avg
Import
375ms
Disk
60MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.390s · 64.3MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.7s · import 0.360s · 59MB
60MB installed
● package 60MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

img2pdf
✓ import img2pdf

This quickstart demonstrates how to convert a single image or a list of image file paths into PDF documents. The `img2pdf.convert()` function handles the conversion, and the result is written to a binary file handle.

import img2pdf import os # Create a dummy image file for demonstration dummy_image_data = bytes([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0A, 0x0A, 0x09, 0x08, 0x09, 0x09, 0x0A, 0x0C, 0x0F, 0x0C, 0x0A, 0x0B, 0x0E, 0x0B, 0x09, 0x09, 0x0D, 0x11, 0x0D, 0x0E, 0x0F, 0x10, 0x10, 0x11, 0x10, 0x0A, 0x0C, 0x12, 0x13, 0x12, 0x10, 0x13, 0x11, 0x11, 0x10, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0x1F, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0x1F, 0x10, 0x00, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0x1F, 0x11, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3F, 0x00, 0xF2, 0xCA, 0x88, 0x64, 0x02, 0xFF, 0xD9]) with open('temp_image.jpg', 'wb') as f: f.write(dummy_image_data) # --- Quickstart --- # Convert a single image file to PDF with open('output_single.pdf', 'wb') as f_pdf: f_pdf.write(img2pdf.convert('temp_image.jpg')) print('Successfully converted single image to output_single.pdf') # Convert multiple images to a multi-page PDF # (for demonstration, we'll just use the same dummy image twice) image_files = ['temp_image.jpg', 'temp_image.jpg'] with open('output_multiple.pdf', 'wb') as f_pdf: f_pdf.write(img2pdf.convert(image_files)) print('Successfully converted multiple images to output_multiple.pdf') # Clean up dummy image os.remove('temp_image.jpg')
img2pdf --version
Debug
Known issues
gotchaPillow's decompression bomb limit can prevent processing of very large images. img2pdf uses Pillow for metadata and conversion of non-JPEG/JPEG2000 images.
fix
Disable the safeguard by passing `--pillow-limit-break` option in CLI or `rotation=img2pdf.Rotation.ifvalid` in library usage, if you understand the risks.
affects: All versions using Pillow
gotchaJPEG images with an invalid EXIF Orientation value of zero (often from Android phones or Canon DSLR cameras) can cause errors.
fix
Fix input images with `exiftool` or similar software, or run img2pdf with `--rotation=ifvalid` (CLI) or pass `rotation=img2pdf.Rotation.ifvalid` to `convert()` (library).
affects: All versions
gotchaWhen converting multiple images from a directory, `os.listdir()` or `os.scandir()` return files in arbitrary order, which can lead to incorrect page sequencing in the final PDF. Ensure images are explicitly sorted before passing to `img2pdf.convert()`.
fix
Sort the list of image file paths (e.g., using `sorted()`) before passing them to `img2pdf.convert()`.
affects: All versions
gotchaCertain TIFF compression methods, especially 'Old-Style JPEG (in tiff)', might not be supported, leading to conversion failures.
fix
If a TIFF file uses unsupported compression, convert it to a different format (like uncompressed TIFF or JPEG) before passing it to img2pdf.
affects: All versions
gotchaIn the command-line usage, the output file argument (`-o`) must generally precede the input image files. Incorrect order can lead to unexpected behavior or errors.
fix
Ensure the command is structured as `img2pdf -o output.pdf input1.jpg input2.png`.
affects: CLI usage
Errors
Common errors & fixes
TypeError: a bytes-like object is required, not 'str'
This error occurs when `img2pdf.convert()` is passed a string that it fails to open as a file (e.g., file not found, permissions), causing it to incorrectly attempt to interpret the string as raw image data, which must be a bytes object.
fix
Ensure the file path provided to `img2pdf.convert()` is correct and accessible. If intending to pass raw image data, provide it as a `bytes` object, not a string.
ModuleNotFoundError: No module named 'img2pdf'
The `img2pdf` package is not installed in the Python environment currently being used, or there is a conflict/misconfiguration with multiple Python installations or their respective package paths.
fix
Install the library using `pip install img2pdf` (or `pip3 install img2pdf`). Verify that the Python interpreter running the script is the same one where the package was installed.
IOError: [Errno 2] No such file or directory: '...' (or FileNotFoundError)
The `img2pdf` library or the underlying file system operation cannot locate the specified image file. This frequently happens when providing only filenames from `os.listdir()` without their full directory path, or if the path is simply incorrect.
fix
Provide the full, absolute path to the image file(s). Use `os.path.join()` or `pathlib.Path` to construct correct and platform-independent file paths.
ValueError: unknown colorspace (or PIL: error reading image: cannot identify)
These errors indicate that the Pillow library, which `img2pdf` uses for image processing, cannot properly read, identify, or process the input image. This can be due to an unsupported image format, a corrupt file, or specific encoding/compression methods (e.g., 'Old-Style JPEG (in tiff)' compression for TIFF files) that are not natively supported by Pillow or `img2pdf`.
fix
Verify the image file is not corrupt and is in a standard, widely supported format (e.g., common JPEG, PNG, or TIFF types). For problematic TIFFs or other complex formats, consider pre-converting them to a simpler format like JPEG or PNG using another tool before feeding them to `img2pdf`. If the issue is related to EXIF orientation, try passing `rotation=img2pdf.Rotation.ifvalid` to the `convert` function.
Upgrade
Version history
0.6.3latest on PyPI · released Nov 5, 2025
Audit
Dependencies
PillowrequiredUsed by img2pdf to obtain image metadata and convert input images if necessary (e.g., non-JPEG/JPEG2000 formats).
pikepdfoptionalCan be used as an alternative rendering engine (supported since 0.4.0).
Agent activity
11 hits · last 30 days
node
10
Resources
img2pdf — pip install img2pdf · libregistry