From 77630f000cdf7efbbd0186702ac9d477677a2cd5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 21 May 2019 06:08:57 +1000 Subject: [PATCH 1/3] Updated resampling filter error messages --- Tests/test_image_transform.py | 9 +++++++++ src/PIL/Image.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index ae1cf6e3d..384657d98 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -160,6 +160,15 @@ class TestImageTransform(PillowTestCase): im = hopper() self.assertRaises(ValueError, im.transform, (100, 100), None) + def test_unknown_resampling_filter(self): + im = hopper() + (w, h) = im.size + for resample in (Image.BOX, "unknown"): + self.assertRaises(ValueError, im.transform, (100, 100), Image.EXTENT, + (0, 0, + w, h), + resample) + class TestImageTransformAffine(PillowTestCase): transform = Image.AFFINE diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b9e569a83..c54e59833 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1794,7 +1794,18 @@ class Image(object): if resample not in ( NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING, ): - raise ValueError("unknown resampling filter") + message = "Unknown resampling filter ("+str(resample)+")." + + filters = [filter[1]+" (%d)" % filter[0] for filter in ( + (NEAREST, "Image.NEAREST"), + (LANCZOS, "Image.LANCZOS"), + (BILINEAR, "Image.BILINEAR"), + (BICUBIC, "Image.BICUBIC"), + (BOX, "Image.BOX"), + (HAMMING, "Image.HAMMING") + )] + raise ValueError( + message+" Use "+", ".join(filters[:-1])+" or "+filters[-1]) size = tuple(size) @@ -2263,7 +2274,22 @@ class Image(object): raise ValueError("unknown transformation method") if resample not in (NEAREST, BILINEAR, BICUBIC): - raise ValueError("unknown resampling filter") + if resample in (BOX, HAMMING, LANCZOS): + message = { + BOX: "Image.BOX", + HAMMING: "Image.HAMMING", + LANCZOS: "Image.LANCZOS/Image.ANTIALIAS" + }[resample]+" (%d) cannot be used." % resample + else: + message = "Unknown resampling filter ("+str(resample)+")." + + filters = [filter[1]+" (%d)" % filter[0] for filter in ( + (NEAREST, "Image.NEAREST"), + (BILINEAR, "Image.BILINEAR"), + (BICUBIC, "Image.BICUBIC") + )] + raise ValueError( + message+" Use "+", ".join(filters[:-1])+" or "+filters[-1]) image.load() From 4fa3016d678d636366dfc37d10d65c297ea0340b Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Thu, 6 Jun 2019 21:22:45 +1000 Subject: [PATCH 2/3] Changed string formatting Co-Authored-By: Hugo --- src/PIL/Image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c54e59833..25450be5c 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1794,7 +1794,7 @@ class Image(object): if resample not in ( NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING, ): - message = "Unknown resampling filter ("+str(resample)+")." + message = "Unknown resampling filter ({}).".format(resample) filters = [filter[1]+" (%d)" % filter[0] for filter in ( (NEAREST, "Image.NEAREST"), @@ -2279,7 +2279,7 @@ class Image(object): BOX: "Image.BOX", HAMMING: "Image.HAMMING", LANCZOS: "Image.LANCZOS/Image.ANTIALIAS" - }[resample]+" (%d) cannot be used." % resample + }[resample]+" ({}) cannot be used.".format(resample) else: message = "Unknown resampling filter ("+str(resample)+")." From a94bd137915032889478efe0ae146a70be42f50d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 6 Jun 2019 21:33:12 +1000 Subject: [PATCH 3/3] Changed string formatting --- src/PIL/Image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 25450be5c..4e84fa53d 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1796,7 +1796,7 @@ class Image(object): ): message = "Unknown resampling filter ({}).".format(resample) - filters = [filter[1]+" (%d)" % filter[0] for filter in ( + filters = ["{} ({})".format(filter[1], filter[0]) for filter in ( (NEAREST, "Image.NEAREST"), (LANCZOS, "Image.LANCZOS"), (BILINEAR, "Image.BILINEAR"), @@ -2281,9 +2281,9 @@ class Image(object): LANCZOS: "Image.LANCZOS/Image.ANTIALIAS" }[resample]+" ({}) cannot be used.".format(resample) else: - message = "Unknown resampling filter ("+str(resample)+")." + message = "Unknown resampling filter ({}).".format(resample) - filters = [filter[1]+" (%d)" % filter[0] for filter in ( + filters = ["{} ({})".format(filter[1], filter[0]) for filter in ( (NEAREST, "Image.NEAREST"), (BILINEAR, "Image.BILINEAR"), (BICUBIC, "Image.BICUBIC")