From 6639b2af7fff7ad7b6b1cfc1421fc1a3ae8206c4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 19 Jun 2021 21:24:58 +1000 Subject: [PATCH] Copy palette to new images in expand --- Tests/test_imageops.py | 6 ++++++ src/PIL/ImageOps.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index ff2445a5168..366b2a3bbc6 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -156,6 +156,12 @@ def test_scale(): assert newimg.size == (25, 25) +def test_expand_palette(): + im = Image.open("Tests/images/hopper.gif") + im_expanded = ImageOps.expand(im) + assert_image_equal(im_expanded.convert("RGB"), im.convert("RGB")) + + def test_colorize_2color(): # Test the colorizing function with 2-color functionality diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index f9c35b2c69f..eb1243770b8 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -393,6 +393,8 @@ def expand(image, border=0, fill=0): width = left + image.size[0] + right height = top + image.size[1] + bottom out = Image.new(image.mode, (width, height), _color(fill, image.mode)) + if image.mode == "P" and image.palette: + out.putpalette(image.palette) out.paste(image, (left, top)) return out