Skip to content
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

Added has_transparency_data #7420

Merged
merged 6 commits into from
Sep 30, 2023
Merged

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Sep 25, 2023

Resolves #7384

Adds a method to determine if an image has transparency data - an alpha channel, an RGBA palette or "transparency" in the image's info directory.

I chose this method name to try and indicate that the image may still be opaque. For example, hopper("RGB").convert("RGBA") has an alpha channel, but has no transparent or translucent pixels.

In doing so, I found a similar check in WebPImagePlugin.

alpha = (
"A" in im.mode
or "a" in im.mode
or (im.mode == "P" and "transparency" in im.info)
)
im = im.convert("RGBA" if alpha else "RGB")

So I've changed that to use this new method.

src/PIL/Image.py Outdated Show resolved Hide resolved
@chinery
Copy link

chinery commented Sep 26, 2023

Hi @radarhere – thanks for this PR! My only question was going to be whether image palettes support modes other than RGBA, but you've addressed that in your comments to @homm, so from my perspective (of admittedly limited experience), I think it looks great.

@radarhere radarhere marked this pull request as ready for review September 26, 2023 08:48
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

This could also be a property, any preference?

src/PIL/Image.py Outdated Show resolved Hide resolved
@radarhere radarhere changed the title Added has_transparency_data() Added has_transparency_data Sep 26, 2023
@radarhere
Copy link
Member Author

Sure, that seems like a reasonable idea. I've added a commit to change it to a property.

@hugovk
Copy link
Member

hugovk commented Sep 29, 2023

Looks good, let's also add to the release notes.

@radarhere
Copy link
Member Author

Sure, I've pushed a commit for that.

Copy link
Member

@hugovk hugovk 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!

@hugovk hugovk merged commit 8cb1e29 into python-pillow:main Sep 30, 2023
51 checks passed
@radarhere radarhere deleted the transparency branch September 30, 2023 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method to test if image has transparency/alpha
4 participants