From d80aa74da45706ddce358130801a653830097581 Mon Sep 17 00:00:00 2001 From: Sitcebelly Date: Sun, 18 Sep 2022 21:14:52 +0300 Subject: [PATCH 1/5] Put palette into the new pad image --- src/PIL/ImageOps.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 0c3f900ca..9a2dd66c9 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -291,6 +291,9 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5 out = resized else: out = Image.new(image.mode, size, color) + palette = image.palette.copy() + if palette: + out.putpalette(palette.palette) if resized.width != size[0]: x = int((size[0] - resized.width) * max(0, min(centering[0], 1))) out.paste(resized, (x, 0)) From d88200e0d091488d13005f85bc70dfaf51ec4e03 Mon Sep 17 00:00:00 2001 From: Sitcebelly Date: Sun, 18 Sep 2022 22:32:09 +0300 Subject: [PATCH 2/5] fix bug --- src/PIL/ImageOps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 9a2dd66c9..b08fc69ae 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -293,7 +293,7 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5 out = Image.new(image.mode, size, color) palette = image.palette.copy() if palette: - out.putpalette(palette.palette) + out.putpalette(palette) if resized.width != size[0]: x = int((size[0] - resized.width) * max(0, min(centering[0], 1))) out.paste(resized, (x, 0)) From c0aaf548161a2ef16147b2dfe1916c789cae962d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Sep 2022 12:41:20 +1000 Subject: [PATCH 3/5] Removed unnecessary palette copy --- src/PIL/ImageOps.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index b08fc69ae..361048d7a 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -291,9 +291,8 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5 out = resized else: out = Image.new(image.mode, size, color) - palette = image.palette.copy() - if palette: - out.putpalette(palette) + if resized.palette: + out.putpalette(resized.palette) if resized.width != size[0]: x = int((size[0] - resized.width) * max(0, min(centering[0], 1))) out.paste(resized, (x, 0)) From 279ddf4ce6c76498ac29df2552a3023b9aaa76c1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Sep 2022 21:34:29 +1000 Subject: [PATCH 4/5] Use getpalette() in ImageOps --- Tests/test_imageops.py | 14 ++++++++++++++ src/PIL/ImageOps.py | 7 +++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 01e40e6d4..367ba7e3e 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -130,6 +130,20 @@ def test_pad(): ) +def test_palette(): + im = hopper("P") + + # Expand + expanded_im = ImageOps.expand(im) + assert_image_equal(im.convert("RGB"), expanded_im.convert("RGB")) + + # Pad + padded_im = ImageOps.pad(im, (256, 128), centering=(0, 0)) + assert_image_equal( + im.convert("RGB"), padded_im.convert("RGB").crop((0, 0, 128, 128)) + ) + + def test_pil163(): # Division by zero in equalize if < 255 pixels in image (@PIL163) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 361048d7a..99f10d739 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -21,7 +21,7 @@ import functools import operator import re -from . import Image +from . import Image, ImagePalette # # helpers @@ -292,7 +292,7 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5 else: out = Image.new(image.mode, size, color) if resized.palette: - out.putpalette(resized.palette) + out.putpalette(resized.getpalette()) if resized.width != size[0]: x = int((size[0] - resized.width) * max(0, min(centering[0], 1))) out.paste(resized, (x, 0)) @@ -399,8 +399,7 @@ def expand(image, border=0, fill=0): height = top + image.size[1] + bottom color = _color(fill, image.mode) if image.mode == "P" and image.palette: - image.load() - palette = image.palette.copy() + palette = ImagePalette.ImagePalette(palette=image.getpalette()) if isinstance(color, tuple): color = palette.getcolor(color) else: From 3c42b270b9acba34192c1c27af624d275c0f3607 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Sep 2022 21:39:38 +1000 Subject: [PATCH 5/5] Copy palette in expand() for PA --- Tests/test_imageops.py | 5 +++-- src/PIL/ImageOps.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 367ba7e3e..1051d9843 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -130,8 +130,9 @@ def test_pad(): ) -def test_palette(): - im = hopper("P") +@pytest.mark.parametrize("mode", ("P", "PA")) +def test_palette(mode): + im = hopper(mode) # Expand expanded_im = ImageOps.expand(im) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 99f10d739..57a032c94 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -398,7 +398,7 @@ def expand(image, border=0, fill=0): width = left + image.size[0] + right height = top + image.size[1] + bottom color = _color(fill, image.mode) - if image.mode == "P" and image.palette: + if image.palette: palette = ImagePalette.ImagePalette(palette=image.getpalette()) if isinstance(color, tuple): color = palette.getcolor(color)