pdf2image

repository·master·Indexed 24 days ago

https://github.com/belval/pdf2image

A Python 3.7+ module that acts as a wrapper around the pdftoppm and pdftocairo command-line tools to convert PDF documents into PIL Image objects or image files. It provides functions like convert_from_path and convert_from_bytes, and requires the poppler library to be installed on the system.

Tokens
2.4K
Snippets
13
Records
21
Agent score
83%

What's inside pdf2image

  1. Overview of pdf2image

    master
    pdf2image is a Python module designed to convert PDF files into images. It functions as a wrapper around the pdftoppm and pdftocairo utilities. To use this library, you must have these underlying utilities installed on your system.
  2. Avoid memory exhaustion using `output_folder`

    master

    When processing large PDFs, converting them directly to memory can exhaust system resources. To prevent this, provide an output_folder argument. This instructs pdf2image to save the converted images to disk. The resulting objects are still compatible with Pillow and will load the image data on demand.

    import tempfile
    from pdf2image import convert_from_path
    
    with tempfile.TemporaryDirectory() as path:
        images_from_path = convert_from_path("/home/user/example.pdf", output_folder=path)
  3. Install pdf2image using Conda

    master

    For a platform-independent installation using Conda, follow these steps:

    1. Install poppler from conda-forge:
      conda install -c conda-forge poppler
    2. Install pdf2image via pip:
      pip install pdf2image
    conda install -c conda-forge poppler
    pip install pdf2image
  4. Optimize pdf2image performance

    master

    To achieve better performance when converting PDFs:

    • Use an output folder: This is significantly faster if you are using an SSD. It also prevents Out-Of-Memory (OOM) errors for large PDFs.
    • Use JPEG format: If I/O is your bottleneck, using fmt='jpeg' can lead to significant gains. Note that PNG is slower due to compression.
    • Thread count: Using multiple threads can provide gains, but avoid using more than 4 threads to prevent I/O bottlenecks.
    • Use pdftocairo: Set use_pdftocairo=True to force the use of pdftocairo, which may improve performance.
    • Use paths_only=True: If you are dealing with very large PDFs and want to avoid memory exhaustion, set paths_only=True to receive file paths instead of PIL Image objects.
  5. Install Poppler for Windows

    master

    Windows users must install poppler. It is recommended to use the @oschwartz10612 version.

    After downloading, you have two options to make it available to pdf2image:

    1. Add the bin/ folder of the poppler installation to your system PATH.
    2. Pass the path directly to the conversion function using the poppler_path argument.

    Example:

    images = convert_from_path(r'C:\path\to\poppler-xx\bin', '/home/belval/example.pdf')
  6. Install Poppler on Windows

    master

    Since Poppler is not available via standard Windows package managers, follow these steps:

    1. Download the latest poppler package from the oschwartz10612 releases.
    2. Extract the directory to a location on your system.
    3. Add the bin/ directory from the extracted folder to your system's PATH environment variable.
    4. Verify the installation by opening cmd and running pdftoppm -h.
  7. Convert PDF to images from bytes

    master

    Use convert_from_bytes to convert PDF data directly from a byte stream. This is useful when the PDF content is already in memory or being read from a stream.

    from pdf2image import convert_from_bytes
    
    with open("/home/user/example.pdf","rb") as pdf:
        images = convert_from_bytes(pdf.read())