Hướng dẫn pillow rotate image python

The Image module of the Python image processing library Pillow(PIL) provides the rotate() method for rotating an image.

  • Image Module — Pillow (PIL Fork) 4.4.0.dev0 documentation

This article describes each parameter of rotate().

  • Rotation angle: angle
  • Resampling Filters: resample
  • Expand output image size: expand
  • Coordinates of the center of rotation: center
  • Translation: translate
  • Color for the outside area: fillcolor

Please refer to the following article for installation and basic usage of Pillow(PIL).

  • How to use Pillow (PIL: Python Imaging Library)

Use flip() and mirror() of the ImageOps module to flip the image vertically or horizontally.

  • Python, Pillow: Flip image

See the following article for image rotation with OpenCV, NumPy.

  • OpenCV, NumPy: Rotate and flip image

Load the images as follows:

from PIL import Image

im = Image.open('data/src/lena.jpg')

Hướng dẫn pillow rotate image python

Rotation angle: angle

In rotate(), specify the rotation angle in degrees as the first parameter angle. The direction of rotation is counterclockwise.

Rotate 90 degrees:

im_rotate = im.rotate(90)

Hướng dẫn pillow rotate image python

Rotate 45 degrees.

im_rotate = im.rotate(45)

Hướng dẫn pillow rotate image python

Resampling Filters: resample

The parameter resample can be used to specify the resampling filter.

  • Image.NEAREST (Nearest neighbor, default)
  • Image.BILINEAR
  • Image.BICUBIC

With Image.BICUBIC, the details are clearer than the default Image.NEAREST.

im_rotate = im.rotate(45, resample=Image.BICUBIC)

Hướng dẫn pillow rotate image python

Expand output image size: expand

As can be seen from the output image of the above example, by default, the size of the output image is equal to the size of the input image, and parts outside the region are truncated.

If you want to keep the whole rotated image, set the parameter expand to True.

im_rotate = im.rotate(90, expand=True)

Hướng dẫn pillow rotate image python

im_rotate = im.rotate(45, expand=True)

Hướng dẫn pillow rotate image python

Coordinates of the center of rotation: center

You can specify the rotation center position with the parameter center. By default, center is the center of the image.

im_rotate = im.rotate(45, center=(0, 60))

Hướng dẫn pillow rotate image python

In the case of expand=True, the output image area is determined assuming that the image is rotated about the image center.

im_rotate = im.rotate(45, center=(0, 60), expand=True)

Hướng dẫn pillow rotate image python

Translation: translate

You can translate before rotation with the parameter translate. translate is specified by (translation distance in x direction, translation distance in y direction).

Without rotation:

im_rotate = im.rotate(0, translate=(100, 50))

Hướng dẫn pillow rotate image python

Rotate 45 degrees and translate:

im_rotate = im.rotate(45, translate=(100, 50))

Hướng dẫn pillow rotate image python

If expand=True, the output image area is determined assuming that the image is rotated without translation.

im_rotate = im.rotate(45, translate=(100, 50), expand=True)

Hướng dẫn pillow rotate image python

Color for the outside area: fillcolor

You can specify the color for the outside area with the fillcolor. The default color is black.

In the case of RGB images, it is specified as a tuple of (R, G, B).

im_rotate = im.rotate(45, fillcolor=(255, 128, 0), expand=True)

Hướng dẫn pillow rotate image python