Repository navigation
fix(html): keep CMYK images and skip undecodable ones instead of failing - #4681
usmanmateen wants to merge 1 commit into
Conversation
ImageResourceLoader.create_image_ref passed images to ImageRef.from_pil without normalizing them, so a CMYK image raised an OSError, which the except clause did not catch. One such image in an HTML or Markdown file made the whole conversion fail. Normalize the image like the Word and PowerPoint backends do since docling-project#4598, and catch OSError so an image that still can't be decoded (e.g. a truncated file) gives a warning and no picture, like the other image errors. Resolves docling-project#4680 Signed-off-by: usmanmateen <usmanmateen255@gmail.com>
|
✅ DCO Check Passed Thanks @usmanmateen, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit Normalize images for PNG output in ImageResourceLoader and catch OSError exceptions to skip unreadable images safely. Topic labels added: CI safety: no concern found. A maintainer must still approve CI. 1 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): no blocking issue found
This review covers the full diff at 8d595de78f. It read the code only. It did not run tests or code from this pull request.
This PR fixes two related problems in ImageResourceLoader.create_image_ref: (1) CMYK images passed to ImageRef.from_pil raised OSError because PNG cannot store CMYK, and (2) other OSError failures (e.g. truncated files) propagated instead of being caught. The fix applies normalize_image_for_png (already used by Word, PowerPoint, OpenDocument, and Excel backends) and adds OSError to the caught exception tuple. Two targeted unit tests are added. The change is minimal and consistent with the existing backend pattern.
A maintainer makes the merge decision. This review never approves the pull request.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@usmanmateen The PR #4683 , which addresses issue #4682 , has been created on this same topic. |
|
cc @PeterStaar-IBM since you reviewed #4598 and the last loader change in #4420. Happy to adjust anything. |
|
@ceberam Thanks for flagging it. #4681 was opened about five minutes before #4683, so I hadn't seen it. I compared the two:
So #4683 covers everything #4681 does, plus MHTML. I'm happy to close this one in favour of #4683. If it helps, |
|
Thanks @usmanmateen for your honest review and your overall contribution to Docling! |
ImageRef.from_pil() serializes to PNG, so images in CMYK, LAB or F mode raise OSError: cannot write mode <mode> as PNG. In the shared ImageResourceLoader.create_image_ref() that error is not in the except tuple, so it escaped and aborted the whole conversion for the HTML, Markdown, AsciiDoc and JATS backends. In the MHTML branch of html_backend the error is caught, so the picture was silently dropped instead. Normalize the image first, as the Office backends already do, and catch OSError so that a single unreadable image can never fail a conversion. Unit test for the truncated-image (OSError) path carried over from docling-project#4681 by @usmanmateen. Co-authored-by: usmanmateen <usmanmateen255@gmail.com> Signed-off-by: Ryan Yang <ryanxys@gmail.com>
Resolves #4680
ImageResourceLoader.create_image_refpassed images toImageRef.from_pilwithout normalizing them, so a CMYK image raised anOSErrorthat theexceptdidn't catch, and one such image in an HTML or Markdown file failed the whole conversion. This normalizes the image withnormalize_image_for_png, the same way the Word and PowerPoint backends do since #4598. It also addsOSErrorto the caught errors, so an image that still can't be decoded (for example a truncated file) gives the usual "Could not process an image" warning and no picture, instead of failing the document.Checklist:
Review evidence:
ConversionStatus.FAILURE ['cannot write mode CMYK as PNG']onmainandConversionStatus.SUCCESS []with this change. The CMYK picture is kept at 200×100, in both HTML and Markdown.FAILURE(Truncated File Read) onmain,SUCCESSwith this change.tests/test_image_resource_loader.py:test_create_image_ref_converts_cmykandtest_create_image_ref_skips_undecodable_image. Both fail onmain(cannot write mode CMYK as PNG, and the truncated image raising instead of warning) and pass here.pytest tests/test_image_resource_loader.py tests/test_backend_html.py tests/test_backend_mhtml.py tests/test_backend_markdown.py: 179 passed, 1 skipped, 2 failed. The 2 failures (test_fetch_refuses_non_public_host[localhost]andtest_fetch_refuses_redirect_to_non_public_host) fail the same way onmainin my environment (WSL2) and are about localhost URL checks this PR doesn't touch.make validate: all hooks pass.