Skip to content

Commit db64393

Browse files
authored
use Image.Resampling namespace for PIL mapping (#1256)
* use `Image.Resampling` namespace for PIL mapping PIL shows a deprecation warning when accessing resampling constants via the `Image` namespace. The suggested namespace is `Image.Resampling`. This commit updates `_pil_interpolation_to_str` to use the `Image.Resampling` namespace. ``` /tmp/ipykernel_11959/698124036.py:2: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead. Image.NEAREST: 'nearest', /tmp/ipykernel_11959/698124036.py:3: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. Image.BILINEAR: 'bilinear', /tmp/ipykernel_11959/698124036.py:4: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead. Image.BICUBIC: 'bicubic', /tmp/ipykernel_11959/698124036.py:5: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead. Image.BOX: 'box', /tmp/ipykernel_11959/698124036.py:6: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead. Image.HAMMING: 'hamming', /tmp/ipykernel_11959/698124036.py:7: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. Image.LANCZOS: 'lanczos', ``` * use new pillow resampling enum only if it exists
1 parent db8e33c commit db64393

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

timm/data/transforms.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,28 @@ def __call__(self, pil_img):
3535
return torch.from_numpy(np_img).to(dtype=self.dtype)
3636

3737

38-
_pil_interpolation_to_str = {
39-
Image.NEAREST: 'nearest',
40-
Image.BILINEAR: 'bilinear',
41-
Image.BICUBIC: 'bicubic',
42-
Image.BOX: 'box',
43-
Image.HAMMING: 'hamming',
44-
Image.LANCZOS: 'lanczos',
45-
}
38+
# Pillow is deprecating the top-level resampling attributes (e.g., Image.BILINEAR) in
39+
# favor of the Image.Resampling enum. The top-level resampling attributes will be
40+
# removed in Pillow 10.
41+
if hasattr(Image, "Resampling"):
42+
_pil_interpolation_to_str = {
43+
Image.Resampling.NEAREST: 'nearest',
44+
Image.Resampling.BILINEAR: 'bilinear',
45+
Image.Resampling.BICUBIC: 'bicubic',
46+
Image.Resampling.BOX: 'box',
47+
Image.Resampling.HAMMING: 'hamming',
48+
Image.Resampling.LANCZOS: 'lanczos',
49+
}
50+
else:
51+
_pil_interpolation_to_str = {
52+
Image.NEAREST: 'nearest',
53+
Image.BILINEAR: 'bilinear',
54+
Image.BICUBIC: 'bicubic',
55+
Image.BOX: 'box',
56+
Image.HAMMING: 'hamming',
57+
Image.LANCZOS: 'lanczos',
58+
}
59+
4660
_str_to_pil_interpolation = {b: a for a, b in _pil_interpolation_to_str.items()}
4761

4862

@@ -181,5 +195,3 @@ def __repr__(self):
181195
format_string += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio))
182196
format_string += ', interpolation={0})'.format(interpolate_str)
183197
return format_string
184-
185-

0 commit comments

Comments
 (0)