Allow RGBA palettes to work with expand()

This commit is contained in:
Andrew Murray 2025-08-06 11:41:26 +10:00
parent 1e76b758f6
commit 35c92308ad
2 changed files with 18 additions and 2 deletions

View File

@ -186,6 +186,21 @@ def test_palette(mode: str) -> None:
)
def test_rgba_palette() -> None:
im = Image.new("P", (1, 1))
red = (255, 0, 0, 255)
translucent_black = (0, 0, 0, 127)
im.putpalette(red + translucent_black, "RGBA")
expanded_im = ImageOps.expand(im, 1, 1)
palette = expanded_im.palette
assert palette is not None
assert palette.mode == "RGBA"
assert expanded_im.convert("RGBA").getpixel((0, 0)) == translucent_black
def test_pil163() -> None:
# Division by zero in equalize if < 255 pixels in image (@PIL163)

View File

@ -499,14 +499,15 @@ def expand(
height = top + image.size[1] + bottom
color = _color(fill, image.mode)
if image.palette:
palette = ImagePalette.ImagePalette(palette=image.getpalette())
mode = image.palette.mode
palette = ImagePalette.ImagePalette(mode, image.getpalette(mode))
if isinstance(color, tuple) and (len(color) == 3 or len(color) == 4):
color = palette.getcolor(color)
else:
palette = None
out = Image.new(image.mode, (width, height), color)
if palette:
out.putpalette(palette.palette)
out.putpalette(palette.palette, mode)
out.paste(image, (left, top))
return out