From be9224f28525211d88e9e769a32bed80a6480cd0 Mon Sep 17 00:00:00 2001 From: Bibin Hashley Date: Tue, 23 Aug 2022 02:57:03 +0530 Subject: [PATCH 01/15] ImageOps.contain function finding new size issue --- src/PIL/ImageOps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 0c3f900caac..61de3b696f9 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -255,11 +255,11 @@ def contain(image, size, method=Image.Resampling.BICUBIC): if im_ratio != dest_ratio: if im_ratio > dest_ratio: - new_height = int(image.height / image.width * size[0]) + new_height = round(image.height / image.width * size[0]) if new_height != size[1]: size = (size[0], new_height) else: - new_width = int(image.width / image.height * size[1]) + new_width = round(image.width / image.height * size[1]) if new_width != size[0]: size = (new_width, size[1]) return image.resize(size, resample=method) From df4bb3460000d222b3ac077dad925c32093f6b32 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 24 Aug 2022 22:32:42 +1000 Subject: [PATCH 02/15] Added test --- Tests/test_imageops.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 01e40e6d4d5..e3d4136517f 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -110,6 +110,16 @@ def test_contain(new_size): assert new_im.size == (256, 256) +def test_contain_round(): + im = Image.new("1", (43, 63), 1) + new_im = ImageOps.contain(im, (5, 7)) + assert new_im.width == 5 + + im = Image.new("1", (63, 43), 1) + new_im = ImageOps.contain(im, (7, 5)) + assert new_im.height == 5 + + def test_pad(): # Same ratio im = hopper() From f9d3ee0f4888f7618071c0a5315c916062e78854 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 24 Aug 2022 22:56:19 +1000 Subject: [PATCH 03/15] Round position in pad() --- Tests/test_imageops.py | 9 +++++++++ src/PIL/ImageOps.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index e3d4136517f..550578f8f7a 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -140,6 +140,15 @@ def test_pad(): ) +def test_pad_round(): + im = Image.new("1", (1, 1), 1) + new_im = ImageOps.pad(im, (4, 1)) + assert new_im.load()[2, 0] == 1 + + new_im = ImageOps.pad(im, (1, 4)) + assert new_im.load()[0, 2] == 1 + + 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 61de3b696f9..ae43fc3bd8e 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -292,10 +292,10 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5 else: out = Image.new(image.mode, size, color) if resized.width != size[0]: - x = int((size[0] - resized.width) * max(0, min(centering[0], 1))) + x = round((size[0] - resized.width) * max(0, min(centering[0], 1))) out.paste(resized, (x, 0)) else: - y = int((size[1] - resized.height) * max(0, min(centering[1], 1))) + y = round((size[1] - resized.height) * max(0, min(centering[1], 1))) out.paste(resized, (0, y)) return out From 9fa421923c7a46827f5f79bca788c44cb57f14c7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 28 Aug 2022 15:58:30 +1000 Subject: [PATCH 04/15] Removed requirement for 256 palette entries --- Tests/test_image.py | 1 + src/PIL/Image.py | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 7cebed127d9..ab945e946f0 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -620,6 +620,7 @@ def test_remap_palette_transparency(self): im_remapped = im.remap_palette([1, 0]) assert im_remapped.info["transparency"] == 1 + assert len(im_remapped.getpalette()) == 6 # Test unused transparency im.info["transparency"] = 2 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 4eb2dead655..e197c018240 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1944,11 +1944,7 @@ def remap_palette(self, dest_map, source_palette=None): m_im = m_im.convert("L") - # Internally, we require 256 palette entries. - new_palette_bytes = ( - palette_bytes + ((256 * bands) - len(palette_bytes)) * b"\x00" - ) - m_im.putpalette(new_palette_bytes, palette_mode) + m_im.putpalette(palette_bytes, palette_mode) m_im.palette = ImagePalette.ImagePalette(palette_mode, palette=palette_bytes) if "transparency" in self.info: From e7fab6abf44faf3bdf45b99c239bf38569a1ece4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 29 Aug 2022 23:20:31 +1000 Subject: [PATCH 05/15] Fixed remapping to palette with duplicate entries --- Tests/test_file_gif.py | 13 +++++++++++++ src/PIL/GifImagePlugin.py | 2 ++ 2 files changed, 15 insertions(+) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 68cb8a36e8d..4e967faec91 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1087,6 +1087,19 @@ def test_palette_save_P(tmp_path): assert_image_equal(reloaded, im) +def test_palette_save_duplicate_entries(tmp_path): + im = Image.new("P", (1, 2)) + im.putpixel((0, 1), 1) + + im.putpalette((0, 0, 0, 0, 0, 0)) + + out = str(tmp_path / "temp.gif") + im.save(out, palette=[0, 0, 0, 0, 0, 0, 1, 1, 1]) + + with Image.open(out) as reloaded: + assert reloaded.convert("RGB").getpixel((0, 1)) == (0, 0, 0) + + def test_palette_save_all_P(tmp_path): frames = [] colors = ((255, 0, 0), (0, 255, 0)) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 2e11df54c0a..40fbaa9b568 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -523,6 +523,8 @@ def _normalize_palette(im, palette, info): index = im.palette.colors[source_color] except KeyError: index = None + if index in used_palette_colors: + index = None used_palette_colors.append(index) for i, index in enumerate(used_palette_colors): if index is None: From 841ba4a940b2b09b5c07a43c7a75ce1266d0f2c9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 30 Aug 2022 08:08:01 +1000 Subject: [PATCH 06/15] Simplified code --- src/PIL/GifImagePlugin.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 40fbaa9b568..20435fe313a 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -519,10 +519,7 @@ def _normalize_palette(im, palette, info): used_palette_colors = [] for i in range(0, len(source_palette), 3): source_color = tuple(source_palette[i : i + 3]) - try: - index = im.palette.colors[source_color] - except KeyError: - index = None + index = im.palette.colors.get(source_color) if index in used_palette_colors: index = None used_palette_colors.append(index) From 9ebf44f8b482954ea3e4c97f4dfcc230bde5c08a Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 7 Sep 2022 14:24:11 +0000 Subject: [PATCH 07/15] Add renovate.json --- renovate.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 renovate.json diff --git a/renovate.json b/renovate.json new file mode 100644 index 00000000000..f9c2c327040 --- /dev/null +++ b/renovate.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:base" + ] +} From bc069ec93901d9879b6768094529e3804d39d063 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 8 Sep 2022 08:37:45 +1000 Subject: [PATCH 08/15] Added renovate.json to MANIFEST --- MANIFEST.in | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST.in b/MANIFEST.in index 26f9401f2d9..08f6dfc0877 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -25,6 +25,7 @@ exclude .coveragerc exclude .editorconfig exclude .readthedocs.yml exclude codecov.yml +exclude renovate.json global-exclude .git* global-exclude *.pyc global-exclude *.so From 01657d128d063d5a848ef68d28df5d3ea5579208 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 8 Sep 2022 20:19:13 +0300 Subject: [PATCH 09/15] Add label to Dependabot PRs --- renovate.json => .github/renovate.json | 3 +++ .pre-commit-config.yaml | 1 + 2 files changed, 4 insertions(+) rename renovate.json => .github/renovate.json (72%) diff --git a/renovate.json b/.github/renovate.json similarity index 72% rename from renovate.json rename to .github/renovate.json index f9c2c327040..9fd3341b430 100644 --- a/renovate.json +++ b/.github/renovate.json @@ -2,5 +2,8 @@ "$schema": "https://docs.renovatebot.com/renovate-schema.json", "extends": [ "config:base" + ], + "labels": [ + "Dependency" ] } diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index eeb4b391ee3..f81bcb956fa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,6 +40,7 @@ repos: rev: v4.3.0 hooks: - id: check-merge-conflict + - id: check-json - id: check-yaml - repo: https://github.com/sphinx-contrib/sphinx-lint From ae833dd62de4196ec82b5a10ce4daec798cd4a99 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 8 Sep 2022 20:19:37 +0300 Subject: [PATCH 10/15] Group GHA updates into a single PR --- .github/renovate.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/renovate.json b/.github/renovate.json index 9fd3341b430..cc8f0225f30 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -5,5 +5,12 @@ ], "labels": [ "Dependency" - ] + ], + "packageRules": [ + { + "groupName": "github-actions", + "matchManagers": ["github-actions"], + "separateMajorMinor": "false" + } + ] } From a7471e9b843f267befea41d64bb1e1c4cce9a554 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 8 Sep 2022 20:19:56 +0300 Subject: [PATCH 11/15] Create update PRs on the first day of the month --- .github/renovate.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/renovate.json b/.github/renovate.json index cc8f0225f30..4341752c3ae 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -12,5 +12,6 @@ "matchManagers": ["github-actions"], "separateMajorMinor": "false" } - ] + ], + "schedule": ["on the first day of the month"] } From f21bc40b236281b69192d90f215b543b032841ff Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 9 Sep 2022 16:24:11 +0300 Subject: [PATCH 12/15] Avoid release days to keep the CI free Can be 1st, 2nd or 15th: https://github.com/python-pillow/Pillow/blob/main/RELEASING.md --- .github/renovate.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/renovate.json b/.github/renovate.json index 4341752c3ae..e378ffc7877 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -13,5 +13,5 @@ "separateMajorMinor": "false" } ], - "schedule": ["on the first day of the month"] + "schedule": ["on the third day of the month"] } From 1bdf6ef7203e97c8c406d074624a000f49b28a6a Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Mon, 19 Sep 2022 10:36:46 +0300 Subject: [PATCH 13/15] Add OpenSSF Best Practices badge --- README.md | 3 +++ docs/index.rst | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 5e9adaf7e95..e7c0ebc5aa5 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ As of 2019, Pillow development is Number of PyPI downloads + OpenSSF Best Practices diff --git a/docs/index.rst b/docs/index.rst index c731e274600..45af4c5714c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -69,6 +69,10 @@ Pillow for enterprise is available via the Tidelift Subscription. `Learn more Date: Mon, 19 Sep 2022 17:22:39 +0300 Subject: [PATCH 14/15] Fix Renovate config --- .github/renovate.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/renovate.json b/.github/renovate.json index e378ffc7877..ec3ccc8a696 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -13,5 +13,5 @@ "separateMajorMinor": "false" } ], - "schedule": ["on the third day of the month"] + "schedule": ["the 3rd day of the month"] } From 291c23f25014355953b3ad63ad85235a996ac8b3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Tue, 20 Sep 2022 07:35:39 +0300 Subject: [PATCH 15/15] Fix Renovate config Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- .github/renovate.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/renovate.json b/.github/renovate.json index ec3ccc8a696..d1d82433553 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -13,5 +13,5 @@ "separateMajorMinor": "false" } ], - "schedule": ["the 3rd day of the month"] + "schedule": ["on the 3rd day of the month"] }