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

Add rawmode argument to Image.getpalette() #6059

Closed
tswast opened this issue Feb 15, 2022 · 3 comments · Fixed by #6061
Closed

Add rawmode argument to Image.getpalette() #6059

tswast opened this issue Feb 15, 2022 · 3 comments · Fixed by #6061
Labels

Comments

@tswast
Copy link

tswast commented Feb 15, 2022

putpalette has a rawmode argument which accepts "RGBA", allowing a palette with some transparent colors. Unfortunately getpalette does not have a similar argument.

In most but not all of the images I'm working with, the first two elements of the palette are both (0, 0, 0), with the first element corresponding to the transparent color (0, 0, 0, 0) and the second corresponding to (0, 0, 0, 255). I was able to figure this out through experimentation, but would prefer to be more precise, as I have a few images where this is not the case.

Current code:

def recolor_poodle(original_path, new_path):
    image = Image.open(original_path)
    image = image.convert("P")
    unused_palette = 127
    ImageDraw.floodfill(image, (31, 21), unused_palette)
    original_palette = image.getpalette()
    new_palette = [0, 0, 0, 0]
    for palette_index in range(1, len(original_palette) // 3):
        old_rgb = [
            original_palette[3 * palette_index + channel_index]
            for channel_index in range(3)
        ]
        channels = [hex(channel)[2:].rjust(2, "0") for channel in old_rgb]
        # padding
        old_color = f"{channels[0]}{channels[1]}{channels[2]}"
        new_color = OLD_TO_NEW.get(old_color)
        if new_color is None:
            new_palette += old_rgb + [255]
        else:
            new_palette += [
                int(new_color[0:2], base=16),
                int(new_color[2:4], base=16),
                int(new_color[4:6], base=16),
                255,
            ]

    arr = numpy.asarray(image)

    im2 = Image.fromarray(arr, mode="P")
    im2.putpalette(new_palette, rawmode="RGBA")
    im2.save(new_path)

Desired code:

def recolor_poodle(original_path, new_path):
    image = Image.open(original_path)
    image = image.convert("P")
    unused_palette = 127
    ImageDraw.floodfill(image, (31, 21), unused_palette)
    original_palette = image.getpalette(rawmode="RGBA")
    new_palette = []
    for palette_index in range(len(original_palette) // 4):
        # Skip mapping transparent colors.
        if original_palette[4 * palette_index + 3] == 0:
            new_palette += [0, 0, 0, 0]
            continue
        old_rgb = [
            original_palette[4 * palette_index + channel_index]
            for channel_index in range(3)
        ]
        channels = [hex(channel)[2:].rjust(2, "0") for channel in old_rgb]
        # padding
        old_color = f"{channels[0]}{channels[1]}{channels[2]}"
        new_color = OLD_TO_NEW.get(old_color)
        if new_color is None:
            new_palette += old_rgb + [255]
        else:
            new_palette += [
                int(new_color[0:2], base=16),
                int(new_color[2:4], base=16),
                int(new_color[4:6], base=16),
                255,
            ]

    arr = numpy.asarray(image)

    im2 = Image.fromarray(arr, mode="P")
    im2.putpalette(new_palette, rawmode="RGBA")
    im2.save(new_path)
@radarhere
Copy link
Member

Hi. While your feature request may still stand, I suggest talking a look at image.palette.colors. I expect that will solve your problem.

@radarhere
Copy link
Member

I've created PR #6061 to resolve this.

@radarhere radarhere changed the title feat: add rawmode argument to Image.getpalette() Add rawmode argument to Image.getpalette() Feb 16, 2022
@CookiePLMonster
Copy link

I believe this satisfies the wish I had in one of the replies in #6027 (comment). Very happy to see this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants