Merge pull request #3859 from radarhere/error

Updated resampling filter error messages
This commit is contained in:
Hugo 2019-06-07 11:13:15 +03:00 committed by GitHub
commit 11059cb684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -160,6 +160,15 @@ class TestImageTransform(PillowTestCase):
im = hopper() im = hopper()
self.assertRaises(ValueError, im.transform, (100, 100), None) 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): class TestImageTransformAffine(PillowTestCase):
transform = Image.AFFINE transform = Image.AFFINE

View File

@ -1794,7 +1794,18 @@ class Image(object):
if resample not in ( if resample not in (
NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING, NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING,
): ):
raise ValueError("unknown resampling filter") message = "Unknown resampling filter ({}).".format(resample)
filters = ["{} ({})".format(filter[1], 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) size = tuple(size)
@ -2263,7 +2274,22 @@ class Image(object):
raise ValueError("unknown transformation method") raise ValueError("unknown transformation method")
if resample not in (NEAREST, BILINEAR, BICUBIC): 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]+" ({}) cannot be used.".format(resample)
else:
message = "Unknown resampling filter ({}).".format(resample)
filters = ["{} ({})".format(filter[1], 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() image.load()