You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Background of rotated png is rendered black #29300
@Shivangbijalwan This works as well. I also tested it with a different .png image, which has some transparent sections already, and there this happens. I think this is also dependent on the image type, however this is sort of beyond my capabilities.
Too me it still seems like unwanted behaviour, but the workaround(s) here are easy enough to solve the issue that it might be ok...
@tacaswell Changing the loding image to RGBA works .....................
When you load an image using Pillow (Image.open()), its format depends on the file type, such as P (palette-based) or LA (luminance-alpha) for PNGs with transparency, or RGB for JPEGs without transparency. These formats might not explicitly include an alpha channel, which represents transparency, leading to issues when matplotlib tries to render or transform the image. If the alpha channel isn’t properly recognized, transparent regions can appear as black, especially during transformations like rotation. By converting the image to RGBA using convert("RGBA"), you ensure each pixel includes an explicit transparency value, which matplotlib can then interpret correctly. This standardization prevents the loss or misinterpretation of transparency, allowing matplotlib to render the image properly, even after transformations or during output saving, such as exporting to a PDF.
@maederan201 You're absolutely correct that the behavior seems tied to the image type and how matplotlib handles transparency for certain formats. When working with PNG images that already include transparency, Pillow and matplotlib can sometimes misinterpret the transparency information due to differences in how formats like P (palette-based) or LA (luminance-alpha) store data. This can lead to unexpected results, such as transparent regions being displayed as black.
While it does feel like unwanted behavior—likely a quirk in how matplotlib processes image transformations—the convert("RGBA") workaround provides a reliable solution. By explicitly standardizing the image format to RGBA, you remove ambiguities and ensure consistent handling of transparency, even with complex images. It's not ideal that this step is needed, but as you've pointed out, the fix is simple and effective enough to mitigate the issue without significant overhead.
@Shivangbijalwan Once we have a numpy array the details of loading from the file should not matter. Presumably we should be able to up-convert to add the alpha channel when we do the rotation. Do you want to try opening a PR to do that?
I also want to remind people to please not just copy-paste output from LLMs into github.
What does your mirror_icon numpy array look like? I've just tried your example, but replacing mirror_icon with an RGBA array, and I can't reproduce the black background.
Activity
maederan201 commentedon Dec 13, 2024
I should have done some more due diligence, the problem is resolved if
interpolation='none'is passed toimshow.So is this expected behavior of the interpolation or still a bug that should be fixed?
tacaswell commentedon Dec 13, 2024
I think we should understand how why this is happening before we decide if it is a bug or a feature ;)
Shivangbijalwan commentedon Dec 17, 2024
you can change the colour of the background convert the mirror icon to RGBA
(Edit: code formatted)
maederan201 commentedon Dec 22, 2024
@Shivangbijalwan This works as well. I also tested it with a different .png image, which has some transparent sections already, and there this happens. I think this is also dependent on the image type, however this is sort of beyond my capabilities.
Too me it still seems like unwanted behaviour, but the workaround(s) here are easy enough to solve the issue that it might be ok...
tacaswell commentedon Dec 23, 2024
What version of pillow do you have installed?
tacaswell commentedon Dec 23, 2024
@Shivangbijalwan Can you explain a bit why changing the loading image to RGBA works?
Shivangbijalwan commentedon Dec 23, 2024
@tacaswell Changing the loding image to RGBA works .....................
When you load an image using Pillow (Image.open()), its format depends on the file type, such as P (palette-based) or LA (luminance-alpha) for PNGs with transparency, or RGB for JPEGs without transparency. These formats might not explicitly include an alpha channel, which represents transparency, leading to issues when matplotlib tries to render or transform the image. If the alpha channel isn’t properly recognized, transparent regions can appear as black, especially during transformations like rotation. By converting the image to RGBA using convert("RGBA"), you ensure each pixel includes an explicit transparency value, which matplotlib can then interpret correctly. This standardization prevents the loss or misinterpretation of transparency, allowing matplotlib to render the image properly, even after transformations or during output saving, such as exporting to a PDF.
Shivangbijalwan commentedon Dec 23, 2024
@maederan201 You're absolutely correct that the behavior seems tied to the image type and how matplotlib handles transparency for certain formats. When working with PNG images that already include transparency, Pillow and matplotlib can sometimes misinterpret the transparency information due to differences in how formats like P (palette-based) or LA (luminance-alpha) store data. This can lead to unexpected results, such as transparent regions being displayed as black.
While it does feel like unwanted behavior—likely a quirk in how matplotlib processes image transformations—the convert("RGBA") workaround provides a reliable solution. By explicitly standardizing the image format to RGBA, you remove ambiguities and ensure consistent handling of transparency, even with complex images. It's not ideal that this step is needed, but as you've pointed out, the fix is simple and effective enough to mitigate the issue without significant overhead.
tacaswell commentedon Dec 23, 2024
@Shivangbijalwan Once we have a numpy array the details of loading from the file should not matter. Presumably we should be able to up-convert to add the alpha channel when we do the rotation. Do you want to try opening a PR to do that?
I also want to remind people to please not just copy-paste output from LLMs into github.
https://matplotlib.org/devdocs/devel/contribute.html#restrictions-on-generative-ai-usage
Shivangbijalwan commentedon Dec 23, 2024
@tacaswell "Thanks for the suggestion, but I’m not able to open a PR for this at the moment. However, I’ll keep it in mind for future improvements."
dstansby commentedon Mar 21, 2025
What does your
mirror_iconnumpy array look like? I've just tried your example, but replacingmirror_iconwith an RGBA array, and I can't reproduce the black background.Fix black corners when rotating RGB images by converting to RGBA in m…
Fixes matplotlib#29300: Convert RGB to RGBA in make_image to avoid ar…
Fix black corners when rotating RGB images by converting to RGBA in
Fix black corners when rotating RGB images by converting to RGBA in
Fix black corners when rotating RGB images by converting to RGBA in
rcomer commentedon Apr 9, 2025
I updated the OP with a self-contained example.
rcomer commentedon Apr 9, 2025
Actually this was fixed by #29776. But perhaps it is worth adding a test for this case?
Fix black corners when rotating RGB images by converting to RGBA in
socram-somel commentedon Apr 12, 2025
@rcomer added a test for this case in PR #29887
anntzer commentedon Jul 24, 2025
I think this is already tested (somewhat subtly) by the downsampling_specle test change in #29776 (see note at the end of #29776 (comment)).