You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep transparency in flattened PNG, WebP and TIFF exports - #181
export_image flattens by default, and _export_to_path did that with GIMP's image.flatten(), which drops the alpha channel and fills transparent pixels with the background color. So any image with transparency exported as PNG (or WebP/TIFF) came out opaque. That's what #16 saw with bg_remove.py, but it wasn't specific to that script: bg_remove_iterative.py, continuous_edit_test.py, batch_export, export_icon_sizes, export_social_media_kit and the PNG of export_web_optimized all went through the same path.
Fix
For PNG, WebP and TIFF, _merge_for_export replaces flatten() on the export copy:
adds alpha to visible layers that lack it (unlocking the copy's layer first if its pixels are locked), since GIMP merges an indexed image whose bottom layer has no alpha onto the background color;
merges the visible layers (CLIP_TO_IMAGE) and resizes the result to the canvas (a lone layer or group keeps its own bounds);
removes the hidden layers, which flatten() dropped and TIFF would otherwise write as pages;
flattens anyway when no pixel is translucent (alpha histogram, range ending below the top bin: 256 bins at 8-bit, 1024 above), so opaque images export exactly as before, without alpha;
falls back to flatten() when the merge fails or returns a layer that already existed (GIMP returns the selected layer, possibly a hidden one, when all visible layers are off the canvas), which gives GIMP's error or the old output.
JPEG and BMP are flattened as before. The demo scripts and continuous_edit_test.py passed 'file_type': 'png', which export_image ignores; they now pass format.
Testing
tests/test_pdb_status.py: stub tests for the merge path (per format, precision, alpha/translucency, locked layer, failed and bogus merges, JPEG/BMP unchanged, flatten=False untouched).
Live (GIMP 3.2, snap), through the real export_image, compared with the old flatten() output: opaque RGB/16-bit/indexed images and all JPEG/BMP exports are pixel-identical to before; transparent RGB, layer masks, 50% opacity, groups, grayscale, indexed (partial layer, gap between layers), 16-bit alpha 0.998 and pixel-locked layers keep their transparency; TIFF page count unchanged; the bg_remove.py exports PNG as RGB (no alpha channel) — transparent background is flattened to white #16 repro now writes an RGBA PNG.
Offline tests all pass; live test_batch_export_live, test_negative_index_live, test_unknown_options_live, test_sprite_sheet_edit_text_live, test_bitmap_composite_live pass.
Summary by CodeRabbit
Bug Fixes
PNG, WebP, and TIFF exports now preserve transparent pixels when merging visible layers. Images without transparency continue to export without an alpha channel.
JPEG and BMP exports continue to use the background color.
Documentation
Clarified how layer merging, transparency, and export formats behave.
_export_to_path flattened its export copy with image.flatten(), which
drops the alpha channel and fills transparent pixels with the background
color, so export_image (flatten=True by default), batch_export,
export_icon_sizes, export_social_media_kit and export_web_optimized's PNG
wrote opaque files. For formats that store alpha, merge the visible
layers instead, remove the hidden ones (TIFF wrote them as pages), resize
the result to the canvas, and flatten only when no pixel is transparent,
so opaque images still export without alpha. JPEG and BMP are flattened
as before. If merging fails (no visible layers), flatten() reports why.
The demo scripts and continuous_edit_test passed 'file_type', which
export_image ignores; pass 'format'.
From the CodeRabbit CLI review. An indexed image's merged layer can lack
alpha, so enlarging it to the canvas made the uncovered area opaque: add
alpha first. The opacity check ended its histogram range at 254/255,
which fits the 256 bins of 8-bit images, but higher precisions have 1024,
so alpha up to about 0.999 counted as opaque and the copy was flattened:
end the range below the top bin for the image's precision.
From a review of the branch. When the bottom visible layer of an indexed
image has no alpha, GIMP merges onto the background color, so the canvas
between layers came out opaque; adding alpha to the merged layer can't
recover it. Add alpha to the visible layers before merging instead.
When every visible layer is off the canvas, merge_visible_layers()
returns the selected layer, maybe a hidden one, which was then exported
in place of the image. Treat a result that isn't a new, visible layer as
a failed merge and flatten the copy, as before.
The export_image docstring said BMP can't store alpha; GIMP's BMP
exporter can. Say that JPEG and BMP are flattened instead.
From the CodeRabbit CLI review. image.duplicate() keeps "Lock pixels",
and add_alpha() refuses a locked layer, so a PNG, WebP or TIFF export of
an image whose visible layer had locked pixels and no alpha failed.
Unlock the copy's layer first; the image's own layer stays locked.
Flattened PNG, WebP, and TIFF exports now merge visible layers and preserve transparency when present. JPEG and BMP continue to flatten using the background color. PNG export requests now use the format parameter.
Changes
Image export behavior
Layer / File(s)
Summary
Prepare flattened exports with transparency gimp-mcp-plugin.py, tests/test_pdb_status.py
PNG, WebP, and TIFF/TIF exports merge visible layers on an export copy. The code adds alpha when needed, removes hidden layers after a successful merge, and flattens when the merge fails or the result has no translucent pixels. Tests cover these paths and confirm that JPEG, JPG, and BMP continue to flatten directly.
sequenceDiagram
participant MCPPlugin
participant ExportCopy
participant FormatExporter
MCPPlugin->>ExportCopy: Prepare copied image by merging visible layers
ExportCopy->>MCPPlugin: Return merged layer or merge result
MCPPlugin->>FormatExporter: Send prepared image for export
Loading
Possibly related PRs
gimp-mcp/gimp-mcp#121: Changes the same export path and format handling, including export preparation for several image formats.
Suggested labels: bug
Suggested reviewers: iyuzhang, vicarc
Merge Risk: Minimal · up to 039b7
PNG, WebP and TIFF exports now keep real transparency, while opaque images and JPEG/BMP exports behave as before. The remaining concern affects only alpha values visually indistinguishable from fully opaque in higher-precision images. No merge-blocking risk is evident.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "Keep transparency in flattened PNG, WebP and TIFF exports" matches the main change. The raw summary and objectives show that PNG, WebP, and TIFF exports now merge visible layers and keep tr…
Linked Issues check
[#16] Objective: PNG exported from bg_remove.py must keep transparency (RGBA), not flatten to an opaque RGB image. At the reviewed head, bg_remove.py exports with format: 'png' (verified in the file).…
Out of Scope Changes check
The changes fall into three groups, and each connects to #16. (1) The shared export-path change in gimp-mcp-plugin.py (_merge_for_export, ALPHA_EXPORT_FORMATS) implements the root-cause fix. Other exp…
Full details: Title check
Explanation
The title "Keep transparency in flattened PNG, WebP and TIFF exports" matches the main change. The raw summary and objectives show that PNG, WebP, and TIFF exports now merge visible layers and keep transparent pixels instead of always flattening. The title is specific and does not list files. It does not mention the JPEG and BMP behavior, which stays unchanged, but that detail is not required for the title to pass.
Full details: Linked Issues check
Explanation
[#16] Objective: PNG exported from bg_remove.py must keep transparency (RGBA), not flatten to an opaque RGB image. At the reviewed head, bg_remove.py exports with format: 'png' (verified in the file). The root cause is the shared _export_to_path path, which called image.flatten(). Per the change summary, gimp-mcp-plugin.py now uses _merge_for_export for PNG, WebP and TIFF. This merges visible layers with alpha, removes hidden layers, and flattens only when no pixel is translucent. Opaque images keep the old output, and failed merges fall back to flatten(). The PR reports a live run of the #16 reproduction that writes an RGBA PNG, and stub tests in tests/test_pdb_status.py cover the merge path. Limit: the plugin diff and the merge code could not be read directly (diff unavailable; the file read was truncated before the export code). This verdict relies on the change summary, the PR description, and the reported live and stub test results.
Full details: Out of Scope Changes check
Explanation
The changes fall into three groups, and each connects to #16. (1) The shared export-path change in gimp-mcp-plugin.py (_merge_for_export, ALPHA_EXPORT_FORMATS) implements the root-cause fix. Other exports that used the same flatten() path (batch_export, export_icon_sizes, export_social_media_kit, PNG of export_web_optimized) change behavior as a direct result of that shared fix. The PR discloses this. JPEG and BMP stay unchanged. (2) The file_type to format change in bg_remove.py, bg_remove_iterative.py, continuous_edit_test.py and agent_edit_demo.py fixes the argument that export_image ignores. The #16 issue names the export_image invocation as a possible cause, so this change is connected. (3) The CHANGELOG entry, the gimp_mcp_server.py docstring update, and the tests in tests/test_pdb_status.py support the fix. No unrelated change was found in the reviewed summary. Limit: the plugin code could not be read directly, so the scope check of that file relies on the change summary and PR description.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
The reason will be displayed to describe this comment to others. Learn more.
Additional comments: 1
gimp-mcp-plugin.py-2643-2644: 1
2643-2644: Minor · Data Integrity & Integration · Quick win
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Do not treat the top histogram bin as fully opaque.
For a 16-bit image with alpha 65534/65535 everywhere, GIMP rounds every alpha value into bin 1023. This range excludes that bin, so translucent becomes zero and Line 2647 removes the alpha channel. The export therefore loses valid transparency. GIMP 3.0 uses rounded histogram bins, not an exact opacity test. (raw.githubusercontent.com)
Use an exact minimum-alpha check before flattening higher-precision copies. Keep flattening the fully opaque control case.
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
Re the additional comment on gimp-mcp-plugin.py 2643-2644 (top histogram bin treated as opaque):
Known and intended: the histogram has 1024 bins above 8-bit, so alpha above about 0.9995 counts as opaque (noted in the code comment). An image where every pixel is in that range exports as RGB instead of RGBA, which looks the same; an exact per-pixel check would cost a full read of every high-precision export for no visible difference. Leaving it as is.
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
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.
Fixes #16.
Problem
export_imageflattens by default, and_export_to_pathdid that with GIMP'simage.flatten(), which drops the alpha channel and fills transparent pixels with the background color. So any image with transparency exported as PNG (or WebP/TIFF) came out opaque. That's what #16 saw withbg_remove.py, but it wasn't specific to that script:bg_remove_iterative.py,continuous_edit_test.py,batch_export,export_icon_sizes,export_social_media_kitand the PNG ofexport_web_optimizedall went through the same path.Fix
For PNG, WebP and TIFF,
_merge_for_exportreplacesflatten()on the export copy:CLIP_TO_IMAGE) and resizes the result to the canvas (a lone layer or group keeps its own bounds);flatten()dropped and TIFF would otherwise write as pages;flatten()when the merge fails or returns a layer that already existed (GIMP returns the selected layer, possibly a hidden one, when all visible layers are off the canvas), which gives GIMP's error or the old output.JPEG and BMP are flattened as before. The demo scripts and
continuous_edit_test.pypassed'file_type': 'png', whichexport_imageignores; they now passformat.Testing
tests/test_pdb_status.py: stub tests for the merge path (per format, precision, alpha/translucency, locked layer, failed and bogus merges, JPEG/BMP unchanged,flatten=Falseuntouched).export_image, compared with the oldflatten()output: opaque RGB/16-bit/indexed images and all JPEG/BMP exports are pixel-identical to before; transparent RGB, layer masks, 50% opacity, groups, grayscale, indexed (partial layer, gap between layers), 16-bit alpha 0.998 and pixel-locked layers keep their transparency; TIFF page count unchanged; the bg_remove.py exports PNG as RGB (no alpha channel) — transparent background is flattened to white #16 repro now writes an RGBA PNG.test_batch_export_live,test_negative_index_live,test_unknown_options_live,test_sprite_sheet_edit_text_live,test_bitmap_composite_livepass.Summary by CodeRabbit