Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
socram-somel wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromsocram-somel:bug_29300

Conversation

socram-somel
Copy link

@socram-somelsocram-somel commentedApr 8, 2025
edited
Loading

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 inAxesImage.make_image. This prevents resampling artifacts (e.g., black corners) when applying affine transformations like rotation, particularly forimshow().

The fix is applied late in the rendering pipeline (insidemake_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-actionsgithub-actionsbot 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 uson gitter for real-time discussion.

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

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

@oscargus
Copy link
Member

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

@rcomerrcomer 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'smain branch. If you do

git fetch upstreamgit 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
ifrgba.dtype==np.uint8:
rgba[:, :,3]=255
else:
rgba[:, :,3]=1.0
returnrgba

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

oscargus reacted with thumbs up emoji
@rcomer
Copy link
Member

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
Thetest_imshow_alpha[png] probably didn't pass either by the floating-point handling or/and the missing the following conditionalpha_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-somelsocram-somel changed the titleFixes #29300 black corners when rotating RGB images by converting to RGBATests #29300 black corners when rotating RGB imagesApr 11, 2025
This test verifies that previously black corners caused by rotatedimages (using imshow with transforms) are no longer present. Itinspects the four inward corners of the rendered figure andasserts that no significant black pixels remain, confirming thatthe fix for GH#29300 is effective.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@rcomerrcomerrcomer left review comments

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Projects
Status: Needs review
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[Bug]: Background of rotated png is rendered black
3 participants
@socram-somel@oscargus@rcomer

[8]ページ先頭

©2009-2025 Movatter.jp