Skip to content

fix(image): flatten transparent images onto white instead of black - #4511

Open
wfatih wants to merge 1 commit into
docling-project:mainfrom
wfatih:fix/image-transparent-background
Open

wfatih wants to merge 1 commit into
docling-project:mainfrom
wfatih:fix/image-transparent-background

Conversation

@wfatih

@wfatih wfatih commented Oct 2, 2026

Copy link
Copy Markdown

ImageDocumentBackend converts every frame with frame.convert("RGB"). That drops the alpha channel and keeps whatever colour is stored under it, and for fully transparent pixels that is usually black. A PNG with dark text on a transparent background (RGBA, LA, or palette + tRNS) therefore becomes an all-black page, and the text is invisible to layout and OCR:

img = Image.new("RGBA", (64, 48), (0, 0, 0, 0))   # transparent background
img.paste((0, 0, 0, 255), (0, 0, 16, 12))          # black "text"
# get_page_image().getpixel((63, 47)) -> (0, 0, 0) on main

This adds _to_rgb(), which composites frames that have an alpha channel or a transparency entry onto a white background before the RGB conversion, the same white a PDF page has behind its content. Frames without transparency still go through a plain convert("RGB"), so their output doesn't change.

The new test, parametrised over RGBA, LA and P + tRNS, checks that the transparent background comes out white and the opaque content stays black. It fails on main (3 failed) and passes with this change. tests/test_backend_image_native.py (30 passed) and tests/test_input_doc.py (24 passed) pass, and no groundtruth changes: the only RGBA test image (a TIFF) is fully opaque.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

@mergify

mergify Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @wfatih, all your commits are properly signed off. 🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant