Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading