@@ -421,6 +421,7 @@ def resize(
421421 Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``,
422422 ``InterpolationMode.NEAREST_EXACT``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are
423423 supported.
424+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
424425 max_size (int, optional): The maximum allowed for the longer edge of
425426 the resized image: if the longer edge of the image is greater
426427 than ``max_size`` after being resized according to ``size``, then
@@ -454,8 +455,12 @@ def resize(
454455 if not torch .jit .is_scripting () and not torch .jit .is_tracing ():
455456 _log_api_usage_once (resize )
456457
457- if not isinstance (interpolation , InterpolationMode ):
458- raise TypeError ("Argument interpolation should be a InterpolationMode" )
458+ if isinstance (interpolation , int ):
459+ interpolation = _interpolation_modes_from_int (interpolation )
460+ elif not isinstance (interpolation , InterpolationMode ):
461+ raise TypeError (
462+ "Argument interpolation should be a InterpolationMode or a corresponding Pillow integer constant"
463+ )
459464
460465 if isinstance (size , (list , tuple )):
461466 if len (size ) not in [1 , 2 ]:
@@ -630,6 +635,7 @@ def resized_crop(
630635 Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``,
631636 ``InterpolationMode.NEAREST_EXACT``, ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are
632637 supported.
638+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
633639 antialias (bool, optional): Whether to apply antialiasing.
634640 It only affects **tensors** with bilinear or bicubic modes and it is
635641 ignored otherwise: on PIL images, antialiasing is always applied on
@@ -726,6 +732,7 @@ def perspective(
726732 interpolation (InterpolationMode): Desired interpolation enum defined by
727733 :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
728734 If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
735+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
729736 fill (sequence or number, optional): Pixel fill value for the area outside the transformed
730737 image. If given a number, the value is used for all bands respectively.
731738
@@ -741,8 +748,12 @@ def perspective(
741748
742749 coeffs = _get_perspective_coeffs (startpoints , endpoints )
743750
744- if not isinstance (interpolation , InterpolationMode ):
745- raise TypeError ("Argument interpolation should be a InterpolationMode" )
751+ if isinstance (interpolation , int ):
752+ interpolation = _interpolation_modes_from_int (interpolation )
753+ elif not isinstance (interpolation , InterpolationMode ):
754+ raise TypeError (
755+ "Argument interpolation should be a InterpolationMode or a corresponding Pillow integer constant"
756+ )
746757
747758 if not isinstance (img , torch .Tensor ):
748759 pil_interpolation = pil_modes_mapping [interpolation ]
@@ -1076,6 +1087,7 @@ def rotate(
10761087 interpolation (InterpolationMode): Desired interpolation enum defined by
10771088 :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
10781089 If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1090+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
10791091 expand (bool, optional): Optional expansion flag.
10801092 If true, expands the output image to make it large enough to hold the entire rotated image.
10811093 If false or omitted, make the output image the same size as the input image.
@@ -1097,15 +1109,19 @@ def rotate(
10971109 if not torch .jit .is_scripting () and not torch .jit .is_tracing ():
10981110 _log_api_usage_once (rotate )
10991111
1112+ if isinstance (interpolation , int ):
1113+ interpolation = _interpolation_modes_from_int (interpolation )
1114+ elif not isinstance (interpolation , InterpolationMode ):
1115+ raise TypeError (
1116+ "Argument interpolation should be a InterpolationMode or a corresponding Pillow integer constant"
1117+ )
1118+
11001119 if not isinstance (angle , (int , float )):
11011120 raise TypeError ("Argument angle should be int or float" )
11021121
11031122 if center is not None and not isinstance (center , (list , tuple )):
11041123 raise TypeError ("Argument center should be a sequence" )
11051124
1106- if not isinstance (interpolation , InterpolationMode ):
1107- raise TypeError ("Argument interpolation should be a InterpolationMode" )
1108-
11091125 if not isinstance (img , torch .Tensor ):
11101126 pil_interpolation = pil_modes_mapping [interpolation ]
11111127 return F_pil .rotate (img , angle = angle , interpolation = pil_interpolation , expand = expand , center = center , fill = fill )
@@ -1147,6 +1163,7 @@ def affine(
11471163 interpolation (InterpolationMode): Desired interpolation enum defined by
11481164 :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
11491165 If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
1166+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
11501167 fill (sequence or number, optional): Pixel fill value for the area outside the transformed
11511168 image. If given a number, the value is used for all bands respectively.
11521169
@@ -1162,6 +1179,13 @@ def affine(
11621179 if not torch .jit .is_scripting () and not torch .jit .is_tracing ():
11631180 _log_api_usage_once (affine )
11641181
1182+ if isinstance (interpolation , int ):
1183+ interpolation = _interpolation_modes_from_int (interpolation )
1184+ elif not isinstance (interpolation , InterpolationMode ):
1185+ raise TypeError (
1186+ "Argument interpolation should be a InterpolationMode or a corresponding Pillow integer constant"
1187+ )
1188+
11651189 if not isinstance (angle , (int , float )):
11661190 raise TypeError ("Argument angle should be int or float" )
11671191
@@ -1177,9 +1201,6 @@ def affine(
11771201 if not isinstance (shear , (numbers .Number , (list , tuple ))):
11781202 raise TypeError ("Shear should be either a single value or a sequence of two values" )
11791203
1180- if not isinstance (interpolation , InterpolationMode ):
1181- raise TypeError ("Argument interpolation should be a InterpolationMode" )
1182-
11831204 if isinstance (angle , int ):
11841205 angle = float (angle )
11851206
@@ -1524,7 +1545,7 @@ def elastic_transform(
15241545 interpolation (InterpolationMode): Desired interpolation enum defined by
15251546 :class:`torchvision.transforms.InterpolationMode`.
15261547 Default is ``InterpolationMode.BILINEAR``.
1527- For backward compatibility integer values ( e.g. ``PIL.Image.NEAREST``) are still acceptable .
1548+ The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well .
15281549 fill (number or str or tuple): Pixel fill value for constant fill. Default is 0.
15291550 If a tuple of length 3, it is used to fill R, G, B channels respectively.
15301551 This value is only used when the padding_mode is constant.
0 commit comments