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 img2pdfVerified import paths — ran on the pinned version, not inferred.
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.
Disable the safeguard by passing `--pillow-limit-break` option in CLI or `rotation=img2pdf.Rotation.ifvalid` in library usage, if you understand the risks.
Fix input images with `exiftool` or similar software, or run img2pdf with `--rotation=ifvalid` (CLI) or pass `rotation=img2pdf.Rotation.ifvalid` to `convert()` (library).
Sort the list of image file paths (e.g., using `sorted()`) before passing them to `img2pdf.convert()`.
If a TIFF file uses unsupported compression, convert it to a different format (like uncompressed TIFF or JPEG) before passing it to img2pdf.
Ensure the command is structured as `img2pdf -o output.pdf input1.jpg input2.png`.
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.
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.
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.
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.