Skip to content

BUGFIX: image_utils.convert_image_to_pdf_bytes fails on RGBA, 16-bit image types #219

@Nancis1130

Description

@Nancis1130

🐛 Describe the bug

Description

Thanks for this project, but I found an error while converting the following image. The error is as follows:

Error 500: {"detail":"Error converting image(s) to PDF: Image with transparency and a bit depth of 16.\nThis is unsupported due to PIL limitations.\nIf you accept a lossy conversion, you can manually convert your images to 8 bit using `convert -depth 8` from imagemagick\nerror: Refusing to work with multiple >8bit channels.\n"}

Example Image

This is a PNG with alpha channel or 16-bit per channel depth, I'm not sure if you can reproduce the error after saving it.
Image

Fix

Update the image_utils.convert_image_to_pdf_bytes function to handle these image type:

def convert_image_to_pdf_bytes(image_files: Union[str, List[str]]) -> bytes:

    if isinstance(image_files, str):
        image_files = [image_files]
    elif not isinstance(image_files, list) or not image_files:
        raise ValueError("image_files must be a non-empty string or list of strings")

    for image_file in image_files:
        if not os.path.exists(image_file):
            raise ValueError(f"File does not exist: {image_file}")

    temp_dir = tempfile.mkdtemp()
    cleaned_files = []

    try:
        for image_path in image_files:
            with Image.open(image_path) as img:
                if img.mode == "RGBA":
                    background = Image.new("RGB", img.size, (255, 255, 255))
                    background.paste(img, mask=img.split()[3])
                    img = background
                elif img.mode == "LA": 
                    img = img.convert("L")
                elif img.mode != "RGB":
                    img = img.convert("RGB") 

                base_name = os.path.splitext(os.path.basename(image_path))[0]
                clean_path = os.path.join(temp_dir, f"{base_name}_cleaned.png")
                img.save(clean_path, format="PNG")
                cleaned_files.append(clean_path)

        result = subprocess.run(
            ["img2pdf"] + cleaned_files,
            check=True,
            capture_output=True
        )
        return result.stdout

    except subprocess.CalledProcessError as e:
        error_msg = e.stderr.decode('utf-8').strip()
        raise RuntimeError(f"Error converting image(s) to PDF: {error_msg}") from e
    finally:
        shutil.rmtree(temp_dir, ignore_errors=True)

Versions

Python 3.11.11

Activity

Nancis1130

Nancis1130 commented on May 29, 2025

@Nancis1130
Author

And, this image couldn't be converted fully. The conversion result is below:

Okay, the user is asking, "What is the capital of Pakistan?" That seems straightforward. Let me start by recalling that Pakistan's capital is Islamabad. I remember it was moved from Karachi to Rawalpindi temporarily and then to Islamabad in the 1960s. Hmm, why would someone ask this? Maybe they're a student working on geography homework, a traveler planning a trip, or just someone curious. Since the question is simple, the user might need a quick, clear answer without extra details. But including a bit of context could be helpful without overwhelming them. I should mention that Islamabad became the capital officially in 1967 and that it's purpose-built. Also, maybe clarify it's located in the north, near Rawalpindi. But keep it concise. Wait, are there any common mistakes? Some people might think Karachi is still the capital since it's the largest city. Including a note about the historical shift could prevent confusion. Is there a deeper need here? The user might be testing my knowledge, but it's unlikely. More probably, they want a reliable answer fast. Ensuring accuracy is key—double-checking dates and names. I'll structure the answer with the capital's name first, then briefly explain the move and location. Ending with the precise year to add credibility. That should cover the basics and address potential misunderstandings. The capital of Pakistan is **Islamabad**.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @aman-17@Nancis1130

        Issue actions

          BUGFIX: image_utils.convert_image_to_pdf_bytes fails on RGBA, 16-bit image types · Issue #219 · allenai/olmocr