Skip to content

ImageOps.expand removes the alpha channel of palettes #9136

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'

Activity

  1. radarhere commented on Aug 6, 2025

    @radarhere
    Member

    I've created #9138 to fix this.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions