Skip to content

Tests #29300 black corners when rotating RGB images #29887

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

socram-somel
Copy link

@socram-somel socram-somel commented Apr 8, 2025

Fix black corners when rotating RGB images by converting to RGBA in make_image

Closes #29300.

This patch ensures that RGB uint8 images (shape HxWx3) are internally converted to RGBA (with opaque alpha) before rendering in AxesImage.make_image. This prevents resampling artifacts (e.g., black corners) when applying affine transformations like rotation, particularly for imshow().

The fix is applied late in the rendering pipeline (inside make_image) to avoid changing behavior for PIL images
or breaking baseline tests that rely on specific colormap or interpolation behavior.

Two tests are added:

- test_rgb_array_converted_to_rgba: ensures RGB NumPy input is upgraded to RGBA
- test_rotate_rgb_image_no_black_background: verifies fix but is marked xfail due to backend resampling limitations

No test baselines are changed. This fix is scoped and backend-compatible.
As the bug has been fixed in #29776, test_rotated_image_corners_no_black is being suggested to verify this behavior.
As this issue did not occur in previous versions, the test acts as a verification step to prevent it from reoccurring in the future.

PR summary

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@oscargus
Copy link
Member

oscargus commented Apr 9, 2025

Thanks! This causes an image test failure. Not sure why, since the provided image seems to have an alpha channel so it shouldn't affect anything (it is the bottom right axes that has a rather small difference for some of the squares).

The tests does't have to parametrized since they are only for png anyway.

I do not get the last test. There should definitely be more than 10 black pixels here? On the test run I was checking, there are 26284 black pixels. Can you please elaborate on this?

Finally, are you sure that this only happens for uint8, not for floating-point?

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.

@socram-somel I think the reason you didn't see the image test failure locally is because your branch is behind Matplotlib's main branch. If you do

git fetch upstream
git rebase upstream/main

that will bring it up to date.

Comment on lines 918 to 919
alpha = np.full(self._A.shape[:2] + (1,), 255, dtype=self._A.dtype)
self._A = np.concatenate((self._A, alpha), axis=2)
Copy link
Member

Choose a reason for hiding this comment

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

There is _rgb_to_rgba already in this module, which will also handle the float case.

def _rgb_to_rgba(A):
"""
Convert an RGB image to RGBA, as required by the image resample C++
extension.
"""
rgba = np.zeros((A.shape[0], A.shape[1], 4), dtype=A.dtype)
rgba[:, :, :3] = A
if rgba.dtype == np.uint8:
rgba[:, :, 3] = 255
else:
rgba[:, :, 3] = 1.0
return rgba

I'm also wondering if this is quite the right place for the fix. Is the problem specific to AxesImage or could it affect other subclasses of _ImageBase?

@rcomer
Copy link
Member

rcomer commented Apr 9, 2025

Hi @socram-somel I just realised the problem has already been fixed. See my comment at #29300 (comment).

@socram-somel
Copy link
Author

@oscargus the problem with the last test(test_rotate_rgb_image_no_black_background_rendering_issue[png]), is that the signature of the function that I implemented only is expected to fail if running on macOs. sys.plataform should be removed, or using this instead @pytest.mark.skipif(sys.platform != "darwin", reason="This test only runs on macOS"). The point with this test was to showcase that still a lot of black pixels still in the image
The test_imshow_alpha[png] probably didn't pass either by the floating-point handling or/and the missing the following condition alpha_param is None or alpha_param == 1. I think that the second option is more likely due the use of a explicit set of alpha done in the test.

@socram-somel
Copy link
Author

Hi @rcomer, all right thank you. I've seen your comment, should I add a test (similar to test_rgb_array_converted_to_rgba) and continue with this PR or should it be closed ?

@socram-somel socram-somel changed the title Fixes #29300 black corners when rotating RGB images by converting to RGBA Tests #29300 black corners when rotating RGB images Apr 11, 2025
This test verifies that previously black corners caused by rotated
images (using imshow with transforms) are no longer present. It
inspects the four inward corners of the rendered figure and
asserts that no significant black pixels remain, confirming that
the fix for GH#29300 is effective.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Background of rotated png is rendered black
3 participants