Repository: oumi-ai/oumi
File: src/oumi/utils/image_utils.py (Python)

Code before:
        _FILE_URL_PREFIX
    ):
        input_pdf_filepath = input_pdf_filepath[len(_FILE_URL_PREFIX) :]

    input_filepath = Path(input_pdf_filepath)
    if not input_filepath.is_file():
        raise ValueError(
            f"PDF path is not a file: {input_filepath}"
            if input_filepath.exists()
            else f"PDF path doesn't exist: {input_filepath}"
        )

    _check_pdf2image_dependency()
    import pdf2image  # pyright: ignore[reportMissingImports]

    page_images = pdf2image.convert_from_path(input_filepath, dpi=dpi)
    num_pages = len(page_images)
    for page_idx in range(num_pages):
        try:
            page_images[page_idx] = convert_pil_image_mode(
                page_images[page_idx], mode=mode
            )
        except Exception:
            logger.error(
                "Failed to convert image mode for PDF page "
                f"{page_idx + 1} or {num_pages}: {input_filepath}"
            )
            raise
    return page_images


def load_pdf_pages_from_bytes(
    pdf_bytes: Optional[bytes], dpi: int = 300, mode: str = DEFAULT_IMAGE_MODE
) -> list[PIL.Image.Image]:
    """Loads PDF pages as PIL images from raw PDF file bytes.

    Args:
        pdf_bytes: PDF file content.
        dpi: Resolution to use for PDF page images (dots per inch).
        mode: The requested image mode e.g., "RGB", "HSV", "RGBA",
            "P" (8-bit pixels, using a color palette).
            For details, see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

    Returns:
        PDF pages as PIL images (PIL.Image.Image).
    """
    if pdf_bytes is None or len(pdf_bytes) == 0:
        raise ValueError("No PDF bytes.")

    _check_pdf2image_dependency()
    import pdf2image  # pyright: ignore[reportMissingImports]

Review comment (suggestion):
```suggestion
                f"{page_idx + 1} of {num_pages}: {input_filepath}"
```

Code after:
        _FILE_URL_PREFIX
    ):
        input_pdf_filepath = input_pdf_filepath[len(_FILE_URL_PREFIX) :]

    input_filepath = Path(input_pdf_filepath)
    if not input_filepath.is_file():
        raise ValueError(
            f"PDF path is not a file: {input_filepath}"
            if input_filepath.exists()
            else f"PDF path doesn't exist: {input_filepath}"
        )

    _check_pdf2image_dependency()
    import pdf2image  # pyright: ignore[reportMissingImports]

    page_images = pdf2image.convert_from_path(input_filepath, dpi=dpi)
    num_pages = len(page_images)
    for page_idx in range(num_pages):
        try:
            page_images[page_idx] = convert_pil_image_mode(
                page_images[page_idx], mode=mode
            )
        except Exception:
            logger.error(
                "Failed to convert image mode for PDF page "
                f"{page_idx + 1} of {num_pages}: {input_filepath}"
            )
            raise
    return page_images


def load_pdf_pages_from_bytes(
    pdf_bytes: Optional[bytes], dpi: int = 300, mode: str = DEFAULT_IMAGE_MODE
) -> list[PIL.Image.Image]:
    """Loads PDF pages as PIL images from raw PDF file bytes.

    Args:
        pdf_bytes: PDF file content.
        dpi: Resolution to use for PDF page images (dots per inch).
        mode: The requested image mode e.g., "RGB", "HSV", "RGBA",
            "P" (8-bit pixels, using a color palette).
            For details, see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

    Returns:
        PDF pages as PIL images (PIL.Image.Image).
    """
    if pdf_bytes is None or len(pdf_bytes) == 0:
        raise ValueError("No PDF bytes.")

    _check_pdf2image_dependency()
    import pdf2image  # pyright: ignore[reportMissingImports]