torchvision.transforms.functional.affine(img: Tensor, angle: float, translate: List[int], scale: float, shear: List[float], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: Optional[List[float]] = None, center: Optional[List[int]] = None) Tensor[source]

Apply affine transformation on the image keeping image center invariant. If the image is torch Tensor, it is expected to have […, H, W] shape, where … means an arbitrary number of leading dimensions.

  • img (PIL Image or Tensor) – image to transform.

  • angle (number) – rotation angle in degrees between -180 and 180, clockwise direction.

  • translate (sequence of python:integers) – horizontal and vertical translations (post-rotation translation)

  • scale (float) – overall scale

  • shear (float or sequence) – shear angle value in degrees between -180 to 180, clockwise direction. If a sequence is specified, the first value corresponds to a shear parallel to the x-axis, while the second value corresponds to a shear parallel to the y-axis.

  • interpolation (InterpolationMode) – Desired interpolation enum defined by torchvision.transforms.InterpolationMode. Default is InterpolationMode.NEAREST. If input is Tensor, only InterpolationMode.NEAREST, InterpolationMode.BILINEAR are supported. The corresponding Pillow integer constants, e.g. PIL.Image.BILINEAR are accepted as well.

  • fill (sequence or number, optional) –

    Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively.


    In torchscript mode single int/float value is not supported, please use a sequence of length 1: [value, ].

  • center (sequence, optional) – Optional center of rotation. Origin is the upper left corner. Default is the center of the image.


Transformed image.

Return type:

PIL Image or Tensor

Examples using affine:

Illustration of transforms

Illustration of transforms


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources