From 014972f27ae639c4d02cc46305034a61a4b4a073 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sun, 2 Jul 2023 02:55:42 +0800 Subject: [PATCH] fix pil deprecated constant --- python/paddle/utils/image_util.py | 7 ++++++- python/paddle/vision/transforms/functional_pil.py | 2 +- test/cpp/inference/api/full_pascalvoc_test_preprocess.py | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/python/paddle/utils/image_util.py b/python/paddle/utils/image_util.py index 42e0488a3e7a88..e6fb0581ba694b 100644 --- a/python/paddle/utils/image_util.py +++ b/python/paddle/utils/image_util.py @@ -30,7 +30,12 @@ def resize_image(img, target_size): resized_size = int(round(img.size[0] * percent)), int( round(img.size[1] * percent) ) - img = img.resize(resized_size, Image.ANTIALIAS) + try: + # For Pillow >= 9.1.0 + LANCZOS = Image.Resampling.LANCZOS + except AttributeError: + LANCZOS = Image.LANCZOS + img = img.resize(resized_size, LANCZOS) return img diff --git a/python/paddle/vision/transforms/functional_pil.py b/python/paddle/vision/transforms/functional_pil.py index 3d816c25742f9f..1a177630d1196e 100644 --- a/python/paddle/vision/transforms/functional_pil.py +++ b/python/paddle/vision/transforms/functional_pil.py @@ -30,7 +30,7 @@ 'lanczos': Image.Resampling.LANCZOS, 'hamming': Image.Resampling.HAMMING, } -except: +except AttributeError: _pil_interp_from_str = { 'nearest': Image.NEAREST, 'bilinear': Image.BILINEAR, diff --git a/test/cpp/inference/api/full_pascalvoc_test_preprocess.py b/test/cpp/inference/api/full_pascalvoc_test_preprocess.py index afe9eedc9e2584..48385edef9d915 100644 --- a/test/cpp/inference/api/full_pascalvoc_test_preprocess.py +++ b/test/cpp/inference/api/full_pascalvoc_test_preprocess.py @@ -44,10 +44,16 @@ TEST_LIST_KEY = "VOCdevkit/VOC2007/ImageSets/Main/test.txt" BIN_FULLSIZE = 5348678856 +try: + # For Pillow >= 9.1.0 + LANCZOS = Image.Resampling.LANCZOS +except AttributeError: + LANCZOS = Image.LANCZOS + def preprocess(img): img_width, img_height = img.size - img = img.resize((RESIZE_W, RESIZE_H), Image.ANTIALIAS) + img = img.resize((RESIZE_W, RESIZE_H), LANCZOS) img = np.array(img) # HWC to CHW if len(img.shape) == 3: