-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
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? |
There was a problem hiding this 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.
lib/matplotlib/image.py
Outdated
alpha = np.full(self._A.shape[:2] + (1,), 255, dtype=self._A.dtype) | ||
self._A = np.concatenate((self._A, alpha), axis=2) |
There was a problem hiding this comment.
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.
matplotlib/lib/matplotlib/image.py
Lines 219 to 230 in 7bf4731
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
?
Hi @socram-somel I just realised the problem has already been fixed. See my comment at #29300 (comment). |
@oscargus the problem with the last test( |
Hi @rcomer, all right thank you. I've seen your comment, should I add a test (similar to |
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.
Fix black corners when rotating RGB images by converting to RGBA in make_imageCloses #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 imagesor 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 limitationsNo 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