From d126f3026cd69f981170f26f31916605000634b3 Mon Sep 17 00:00:00 2001 From: Johannes L Date: Sun, 8 May 2022 11:20:49 +0200 Subject: [PATCH] [Enhancement] Add support for latest Pillow resampling filter naming scheme (#1931) * Add support for latest Pillow resampling filter naming scheme * Comments added * Shortened comments, lint fixes * Added specific Pillow version --- mmcv/image/geometric.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mmcv/image/geometric.py b/mmcv/image/geometric.py index cf62ada73..4c423bf2a 100644 --- a/mmcv/image/geometric.py +++ b/mmcv/image/geometric.py @@ -37,15 +37,27 @@ cv2_interp_codes = { 'lanczos': cv2.INTER_LANCZOS4 } +# Pillow >=v9.1.0 use a slightly different naming scheme for filters. +# Set pillow_interp_codes according to the naming scheme used. if Image is not None: - pillow_interp_codes = { - 'nearest': Image.NEAREST, - 'bilinear': Image.BILINEAR, - 'bicubic': Image.BICUBIC, - 'box': Image.BOX, - 'lanczos': Image.LANCZOS, - 'hamming': Image.HAMMING - } + if hasattr(Image, 'Resampling'): + pillow_interp_codes = { + 'nearest': Image.Resampling.NEAREST, + 'bilinear': Image.Resampling.BILINEAR, + 'bicubic': Image.Resampling.BICUBIC, + 'box': Image.Resampling.BOX, + 'lanczos': Image.Resampling.LANCZOS, + 'hamming': Image.Resampling.HAMMING + } + else: + pillow_interp_codes = { + 'nearest': Image.NEAREST, + 'bilinear': Image.BILINEAR, + 'bicubic': Image.BICUBIC, + 'box': Image.BOX, + 'lanczos': Image.LANCZOS, + 'hamming': Image.HAMMING + } def imresize(img,