Created
June 3, 2026 13:23
-
-
Save kanishmalviya/507c79e196cd7aee6b01743968422bcf to your computer and use it in GitHub Desktop.
tiff-to-pdf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| TIFF to PDF Converter | |
| Convert single-page or multi-page TIFF/TIF files into a PDF. | |
| Features: | |
| - Supports .tif and .tiff files | |
| - Supports multi-page TIFFs | |
| - Handles truncated/corrupted TIFFs when possible | |
| - Preserves page order | |
| -------------------------------------------------- | |
| Setup | |
| -------------------------------------------------- | |
| 1. Create virtual environment | |
| Windows: | |
| python -m venv venv | |
| Activate: | |
| venv\Scripts\activate | |
| 2. Install dependencies | |
| pip install pillow | |
| 3. Run | |
| python main.py | |
| 4. Update input/output paths | |
| INPUT_TIFF = r"sample.tif" | |
| OUTPUT_PDF = r"sample.pdf" | |
| -------------------------------------------------- | |
| Expected Output | |
| -------------------------------------------------- | |
| PDF created successfully: | |
| sample.pdf | |
| -------------------------------------------------- | |
| """ | |
| from pathlib import Path | |
| from PIL import Image, ImageFile | |
| # Allow partially corrupted TIFF files | |
| ImageFile.LOAD_TRUNCATED_IMAGES = True | |
| def tiff_to_pdf(tiff_path: str, pdf_path: str) -> None: | |
| """ | |
| Convert TIFF/TIF file to PDF. | |
| Args: | |
| tiff_path: Input TIFF file path | |
| pdf_path: Output PDF file path | |
| """ | |
| tiff_file = Path(tiff_path) | |
| pdf_file = Path(pdf_path) | |
| if not tiff_file.exists(): | |
| raise FileNotFoundError( | |
| f"TIFF file not found: {tiff_file}" | |
| ) | |
| try: | |
| img = Image.open(tiff_file) | |
| except Exception as exc: | |
| raise RuntimeError( | |
| f"Failed to open TIFF file: {exc}" | |
| ) from exc | |
| pages = [] | |
| try: | |
| frame_count = getattr(img, "n_frames", 1) | |
| for frame_index in range(frame_count): | |
| img.seek(frame_index) | |
| page = ( | |
| img.convert("RGB") | |
| if img.mode != "RGB" | |
| else img.copy() | |
| ) | |
| pages.append(page) | |
| except Exception as exc: | |
| raise RuntimeError( | |
| f"Failed while reading TIFF pages: {exc}" | |
| ) from exc | |
| if not pages: | |
| raise RuntimeError( | |
| "No pages found in TIFF file" | |
| ) | |
| try: | |
| pages[0].save( | |
| pdf_file, | |
| format="PDF", | |
| save_all=True, | |
| append_images=pages[1:] | |
| ) | |
| except Exception as exc: | |
| raise RuntimeError( | |
| f"Failed to generate PDF: {exc}" | |
| ) from exc | |
| print(f"PDF created successfully: {pdf_file}") | |
| if __name__ == "__main__": | |
| INPUT_TIFF = "sample.tif" | |
| OUTPUT_PDF = "sample.pdf" | |
| tiff_to_pdf( | |
| INPUT_TIFF, | |
| OUTPUT_PDF | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment