fix: convert RGBA to RGB before saving as JPEG#214
Open
Maigic-AI wants to merge 1 commit into
Open
Conversation
PIL cannot write RGBA images as JPEG. When the source image is PNG with alpha channel, cropped regions retain RGBA mode and fail to save. Convert to RGB before saving to handle this case.
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
cannot write mode RGBA as JPEGerror when saving cropped image regions from PNG sources with alpha channel.Convert RGBA PIL images to RGB before saving as JPEG in
BaseParserResult._save_json_and_markdown().Why
When the source image is a PNG with an alpha channel (RGBA mode), cropped regions retain the RGBA mode through the pipeline:
crop_image_region()inimage_utils.pycrops from the original image, preserving RGBA modeResultFormatter.process()inresult_formatter.pyassigns hardcoded.jpgfilenames to these cropped imagesBaseParserResult._save_json_and_markdown()inbase.pycallsimg.save()on RGBA images with a.jpgpath — PIL cannot write RGBA as JPEG, raisingOSError: cannot write mode RGBA as JPEGThe RGBA-to-RGB conversion discards the alpha channel, which is acceptable because JPEG does not support transparency anyway. The white background from
convert("RGB")is a reasonable default.Validation
glmocr parse ./test.png --config ./config.yamlusing a PNG input with alpha channel — all 5 cropped images saved successfully without errorsoutput/test/imgs/directoryScope
This PR intentionally keeps the fix narrow:
base.py.jpghardcoded inresult_formatter.pyandmarkdown_utils.py)Those can be handled separately if needed.