Skip to content

ImageOps.expand removes the alpha channel of palettes #9136

@gguerreiro

Description

@gguerreiro

Calling ImageOps.expand on a image with an RGBA palette returns an image with an RGB palette.

Here's an example:

from PIL import Image, ImageOps

img = Image.new("P", (16, 16), 1)

img.putpalette((
    255, 0, 0, 0,  # TRANSPARENT RED
    0, 0, 0, 255,  # OPAQUE BLACK
), "RGBA")

framed_img = ImageOps.expand(img, 6, 0)

In this example I created an image of 16 × 16 pixels and with the second color of the palette (index 1), then I call ImageOps.expand to add a border with the first color of the palette (index 0).

I expected the second image to have a transparent border, but it is opaque:

Image (img)

Image (framed_img)

We can easily verify that both images have palettes and the palettes have different modes in a Python REPL:

>>> img.palette.mode
'RGBA'
>>> framed_img.palette.mode
'RGB'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions