{"id":3517,"library":"img2pdf","title":"img2pdf: Lossless Image to PDF Conversion","description":"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.","status":"active","version":"0.6.3","language":"en","source_language":"en","source_url":"https://github.com/josch/img2pdf","tags":["image","pdf","conversion","lossless","jpeg","png","tiff","document generation"],"install":[{"cmd":"pip install img2pdf","lang":"bash","label":"Install img2pdf"}],"dependencies":[{"reason":"Used by img2pdf to obtain image metadata and convert input images if necessary (e.g., non-JPEG/JPEG2000 formats).","package":"Pillow","optional":false},{"reason":"Can be used as an alternative rendering engine (supported since 0.4.0).","package":"pikepdf","optional":true}],"imports":[{"symbol":"img2pdf","correct":"import img2pdf"}],"quickstart":{"code":"import img2pdf\nimport os\n\n# Create a dummy image file for demonstration\ndummy_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])\nwith open('temp_image.jpg', 'wb') as f:\n    f.write(dummy_image_data)\n\n# --- Quickstart --- \n\n# Convert a single image file to PDF\nwith open('output_single.pdf', 'wb') as f_pdf:\n    f_pdf.write(img2pdf.convert('temp_image.jpg'))\nprint('Successfully converted single image to output_single.pdf')\n\n# Convert multiple images to a multi-page PDF\n# (for demonstration, we'll just use the same dummy image twice)\nimage_files = ['temp_image.jpg', 'temp_image.jpg']\nwith open('output_multiple.pdf', 'wb') as f_pdf:\n    f_pdf.write(img2pdf.convert(image_files))\nprint('Successfully converted multiple images to output_multiple.pdf')\n\n# Clean up dummy image\nos.remove('temp_image.jpg')\n","lang":"python","description":"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."},"warnings":[{"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.","message":"Pillow's decompression bomb limit can prevent processing of very large images. img2pdf uses Pillow for metadata and conversion of non-JPEG/JPEG2000 images.","severity":"gotcha","affected_versions":"All versions using Pillow"},{"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).","message":"JPEG images with an invalid EXIF Orientation value of zero (often from Android phones or Canon DSLR cameras) can cause errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Sort the list of image file paths (e.g., using `sorted()`) before passing them to `img2pdf.convert()`.","message":"When 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()`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If a TIFF file uses unsupported compression, convert it to a different format (like uncompressed TIFF or JPEG) before passing it to img2pdf.","message":"Certain TIFF compression methods, especially 'Old-Style JPEG (in tiff)', might not be supported, leading to conversion failures.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the command is structured as `img2pdf -o output.pdf input1.jpg input2.png`.","wrong":"img2pdf input1.jpg input2.png -o output.pdf","message":"In 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.","severity":"gotcha","affected_versions":"CLI usage"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}