镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

ImagePalette: loading a translucent RGBA color into an RGB palette will break the palette #6652

Closed

Description

@jsbueno

This is a minor thing:

In PIL/ImagePalette.py, we have

if len(color) == 4 and color[3] == 255:
color = color[:3]

And then, this color is consumed in line 151, always assuming it is 3-bytes long (and having 4 would make colors from that point on "out of sync") . I was about to send a small PR raising value error in the unchecked case:

   if len(color) == 4 and color[3] < 255:
          raise ValueError(...)

But then, why not just truncate the alpha away, and always use the RGB value if an RGBA color is added to an RGB palette?
Can we do that?

(the sole purpose of this issue is to ask this question - I should submit fix + test afterwards)

Activity

  1. radarhere commented on Oct 9, 2022

    @radarhere
    Member

    For context, the code to handle opaque RGBA colors was added in #5552.

    I think that raising a ValueError is the better solution. If Pillow just accepted the input, this could surprise the user later when they discover that the RGBA color isn't really there.

  2. added a commit that references this issue on Oct 9, 2022
    f9a3178
  3. jsbueno commented on Oct 9, 2022

    @jsbueno
    ContributorAuthor

    Oh, -
    i did the ValueError thing - but it looks like event he code in texts expect to be able to insert translucent colors,
    and get the alpha rounded.

    Tests/test_image_access.py::TestCffi::test_p_putpixel_rgb_rgba[P] FAILED [ 31%]
    
    =================================== FAILURES ===================================
    _____________________ TestCffi.test_p_putpixel_rgb_rgba[P] _____________________
    
    self = <Tests.test_image_access.TestCffi object at 0x00007fb00ce8b8a0>
    mode = 'P'
    
        @pytest.mark.parametrize("mode", ("P", "PA"))
        def test_p_putpixel_rgb_rgba(self, mode):
            for color in [(255, 0, 0), (255, 0, 0, 127)]:
                im = Image.new(mode, (1, 1))
                access = PyAccess.new(im, False)
    >           access.putpixel((0, 0), color)
    
    Tests/test_image_access.py:351: 
    (...)
    E                       ValueError: RGB ImagePalette can't handle non-opaque RGBA colors
    

    So - do we use the RGB components?
    Or should the failing tests be changed?

  4. radarhere commented on Oct 10, 2022

    @radarhere
    Member

    The error you're hitting is when the tests try to set an RGBA value for a P image. The test was changed to use a translucent value in #6504, which was a PR about allowing RGB and RGBA values for PA images. Setting a translucent value for a P image was a side-effect, not the intention. So I'm happy to change the test.

    I've created jsbueno#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions