Skip to content

fix: convert RGBA PNG to RGB in labelme2coco.py#1830

Merged
wkentaro merged 1 commit intomainfrom
fix/labelme2coco-rgba-png
Mar 6, 2026
Merged

fix: convert RGBA PNG to RGB in labelme2coco.py#1830
wkentaro merged 1 commit intomainfrom
fix/labelme2coco-rgba-png

Conversation

@wkentaro
Copy link
Owner

@wkentaro wkentaro commented Mar 5, 2026

Summary

When labelme2coco.py loads a PNG with an alpha channel (RGBA, 4 channels), imgviz.io.imsave() fails with an OSError because JPEG format does not support an alpha channel.

Fix

After loading the image array, strip the alpha channel when the image has 4 channels:

img = labelme.utils.img_data_to_arr(label_file.imageData)
if img.ndim == 3 and img.shape[2] == 4:
    img = img[:, :, :3]

Changes

  • examples/instance_segmentation/labelme2coco.py: drop alpha channel for RGBA images before saving

Closes #1653

RGBA PNGs (4-channel images with an alpha channel) cause an OSError
when imgviz.io.imsave() tries to save them as JPEG, since JPEG does
not support an alpha channel.

Strip the alpha channel by slicing to [:, :, :3] after loading the
image array when the image has 4 channels.

Closes #1653
@wkentaro wkentaro force-pushed the fix/labelme2coco-rgba-png branch from a86af8e to c2e4c88 Compare March 6, 2026 13:26
@wkentaro wkentaro self-assigned this Mar 6, 2026
@wkentaro wkentaro added the fix label Mar 6, 2026
@wkentaro wkentaro added this to the v6.0.0 milestone Mar 6, 2026
@wkentaro wkentaro merged commit defd630 into main Mar 6, 2026
5 checks passed
@wkentaro wkentaro deleted the fix/labelme2coco-rgba-png branch March 6, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant