Skip to content

fix: convert RGBA to RGB before saving as JPEG#214

Open
Maigic-AI wants to merge 1 commit into
zai-org:mainfrom
Maigic-AI:fix/rgba-to-jpeg-save
Open

fix: convert RGBA to RGB before saving as JPEG#214
Maigic-AI wants to merge 1 commit into
zai-org:mainfrom
Maigic-AI:fix/rgba-to-jpeg-save

Conversation

@Maigic-AI

@Maigic-AI Maigic-AI commented May 16, 2026

Copy link
Copy Markdown

Summary

Fix cannot write mode RGBA as JPEG error 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:

  1. crop_image_region() in image_utils.py crops from the original image, preserving RGBA mode
  2. ResultFormatter.process() in result_formatter.py assigns hardcoded .jpg filenames to these cropped images
  3. BaseParserResult._save_json_and_markdown() in base.py calls img.save() on RGBA images with a .jpg path — PIL cannot write RGBA as JPEG, raising OSError: cannot write mode RGBA as JPEG

The 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

  • Tested with glmocr parse ./test.png --config ./config.yaml using a PNG input with alpha channel — all 5 cropped images saved successfully without errors
  • Output files confirmed in output/test/imgs/ directory

Scope

This PR intentionally keeps the fix narrow:

  • only adds a one-line RGBA→RGB conversion at the save point in base.py
  • no changes to filename generation logic (.jpg hardcoded in result_formatter.py and markdown_utils.py)
  • no changes to image loading or cropping pipeline

Those can be handled separately if needed.

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.
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