Repository: open-mmlab/mmcv

Issue:
mmcv, geometry.py not compatible with latest Pillow 9.1.0 (and possibly earlier versions)
Hello,

I am using MMDeploy to generate an optimized model trained by MMDetection.

It looks like there is an incompatibility with latest Pillow. I get the following error when executing MMDeploy `tools/deploy.py`. The error occurs in mmcv `geometric.py`

```
Traceback (most recent call last):
  File "tools/deploy.py", line 7, in <module>
    import mmcv
  File "/home/nvidia/git/project/venv-mmdet/lib/python3.6/site-packages/mmcv/__init__.py", line 5, in <module>
    from .image import *
  File "/home/nvidia/git/project/venv-mmdet/lib/python3.6/site-packages/mmcv/image/__init__.py", line 5, in <module>
    from .geometric import (cutout, imcrop, imflip, imflip_, impad,
  File "/home/nvidia/git/project/venv-mmdet/lib/python3.6/site-packages/mmcv/image/geometric.py", line 42, in <module>
    'nearest': Image.NEAREST,
AttributeError: module 'PIL.Image' has no attribute 'NEAREST'
```

This error does not occur on an earlier version of Pillow (7.0.0) but it occurs on 9.1.0. 
I can replicate the issue by calling Python3 directly:

* Pillow 7.0.0
```
>>> from PIL import Image
>>> Image.NEAREST
0
```

* Pillow 9.1.0
```
from PIL import Image
>>> Image.NEAREST
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'PIL.Image' has no attribute 'NEAREST'
```

Solution:
It looks like the NEAREST... variables has been moved into `PIL.Image.Resampling`: 
[PIL.Image.Resampling.NEAREST](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Resampling.NEAREST)

Alternatively, the deprecated filters can be accessed using the following names:
```
PIL.Image.NONE = Resampling.NEAREST
PIL.Image.LINEAR = Resampling.BILINEAR
PIL.Image.CUBIC = Resampling.BICUBIC
PIL.Image.ANTIALIAS = Resampling.LANCZOS
```


Discussion:
@tehkillerbee Thanks for noticing the version confilct problem and offering possible solution! If possible,could you pull a request to fix above problem?
@imabackstabber Sure, I can try to do a PR. I assume we still need to be backwards compatible with the older versions of pillow?
> . I assume we still need to be backwards compatible with the older versions

Hi @tehkillerbee , looking forward to your coming PR. We need to keep it backward compatible.
Hi @tehkillerbee ,any progress?
@imabackstabber Sorry, I have not updated it yet (been busy with work). But I will try to look at it this weekend and will make sure it is backwards compatible.
@tehkillerbee Thanks for prompted reply! And Also, feel free to let us know if there's any problem when updating and we're always ready for help contributing!

Resolving patch:
diff --git a/mmcv/image/geometric.py b/mmcv/image/geometric.py
--- a/mmcv/image/geometric.py
+++ b/mmcv/image/geometric.py
@@ -37,15 +37,27 @@ def _scale_size(size, scale):
     '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,


Test patch: