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

Improved conversion from RGB to RGBa, LA and La #7888

Merged
merged 3 commits into from Mar 28, 2024

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Mar 21, 2024

#7209 has started support from RGB to RGBa and La, but doesn't consider transparency.

  1. Add support for conversion from RGB to RGBa

When Pillow converts from RGB to RGBA, if there is an info["transparency"] entry, the pixels matching that value have their alpha channel set to zero.

This PR adds support for converting from RGB to RGBa in a similar way, except that the matching pixels have all channels set to zero, because the alpha is premultiplied, and zero alpha means every channel should be zero.

https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

Premultiplied alpha is where the values for each other channel have been multiplied by the alpha. For example, an RGBA pixel of (10, 20, 30, 127) would convert to an RGBa pixel of (5, 10, 15, 127). The values of the R, G and B channels are halved as a result of the half transparency in the alpha channel.

  1. Use transparency info when converting from RGB to LA

Currently in ImagingConvertTransparent, the image is being converted from an image that may or may not be RGB, and the RGB transparency relates to the final image. From RGB to LA, the original image is RGB and the final image is not. So that additional path has been added.

  1. Add support for conversion from RGB to La

Now that RGB to LA works with transparency, converting from RGB to La is similar, except like 1 it premultiplies the values for transparent pixels so that the channels are all zero.

@radarhere radarhere changed the title Support conversion from RGB to RGBa Improved conversion from RGB to RGBa, LA and La Mar 26, 2024
@hugovk hugovk merged commit d734c8b into python-pillow:main Mar 28, 2024
59 checks passed
@radarhere radarhere deleted the convert_rgb branch March 28, 2024 19:28
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.

None yet

2 participants