Skip to content

Fix: support alpha array for RGB images in imshow (closes #26092) #30111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Vaishnavi-Raghupathi
Copy link

-->Summary

This PR fixes issue #26092 by enabling the use of a 2D alpha array with RGB images in imshow. Previously, passing an alpha array worked for grayscale images but was ignored for RGB images. With this change, the alpha array is correctly combined with the RGB data, allowing for per-pixel transparency in color images.

--> What was changed

  • Modified the image handling logic to stack the user-provided alpha array with the RGB image, producing an RGBA image as expected.
  • Shape validation was added to ensure the alpha array matches the image dimensions.

--> How it was tested

  • Manually tested by displaying an RGB image with a 2D alpha array and confirming that transparency is applied correctly (top half transparent, bottom half opaque).

--> Additional notes

  • No new automated test was added, as this was a manual verification. Please let me know if a formal test is required.

Closes #26092

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Vaishnavi-Raghupathi we do usually require new tests for code changes. Also, some of the existing tests are broken by this change, which needs to be addressed.

The issue has moved on a fair bit since it was opened. I believe the most important comment to read is #26092 (comment). You should check what the code in there produces with and without your change. I believe the remaining work is to make the bottom right subplot look like the centre right subplot, i.e. when we have an RGBA array and an alpha array passed in, the alpha of these two arrays should be blended.

# user-specified array alpha overrides the existing alpha channel
A = np.dstack([A[..., :3], alpha])
else: # Scalar alpha
if A.shape[2] == 3: # broadcast scalar alpha
A = np.dstack([A, np.full(A.shape[:2], alpha, np.float32)])
else: # or apply scalar alpha to existing alpha channel
post_apply_alpha = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

post_apply_alpha = True
#to handle RGBA inputs
if A.shape[2] == 4 and alpha is not None:
A[..., 3] = alpha # override existing alpha channel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just repeating what already happened on line 518.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes do not belong in this PR.

@rcomer rcomer moved this from Needs review to Waiting for author in First Time Contributors May 28, 2025
@rcomer
Copy link
Member

rcomer commented Jun 1, 2025

I'm going to mark this as "draft" for now. Please mark it as "ready for review" when you are ready. In the meantime, if you need help, feel free to ask questions here. Or you may prefer to ask them in our incubator gitter room.

@rcomer rcomer marked this pull request as draft June 1, 2025 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

[Bug]: alpha array-type not working with RGB image in imshow()
2 participants