Rotate Image | OpenCV vs Pillow | Python

In this tutorial, you will learn how to rotate an image in OpenCV (cv2) and Pillow (PIL).

1. Rotate by 90, 180, or 270 Degrees

Rotate an OpenCV image by 90, 180, or 270 degrees
Rotate an OpenCV image by 90, 180, or 270 degrees

For OpenCV, the function cv2.rotate lets you rotate the image only by 90, 180, or 270 degrees.

# rotate 90 degrees clockwise
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_90_CLOCKWISE)
# rotate 180 degrees
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_180)
# rotate 270 degrees clockwise (90 degrees counterclockwise)
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_90_COUNTERCLOCKWISE)

2. Rotate by Any Angle

Rotate by any angle
Rotate by any angle

OpenCV

# rotate 45 degrees counterclockwise
h, w = cv2_img.shape[:2]
M = cv2.getRotationMatrix2D(center=(w/2, h/2), angle=45, scale=1.0)
rotated_cv2_img = cv2.warpAffine(cv2_img, M, (w, h))

Pillow

# rotate 45 degrees counterclockwise
rotated_pil_img = pil_img.rotate(45)

Full Example

OpenCV

import cv2

# read image
cv2_img = cv2.imread("test_images/test1.jpg")

# rotate 45 degrees counterclockwise
h, w = cv2_img.shape[:2]
M = cv2.getRotationMatrix2D(center=(w/2, h/2), angle=45, scale=1.0)
rotated_cv2_img = cv2.warpAffine(cv2_img, M, (w, h))

# show the rotated image
cv2.imshow("cv2 rotated image", rotated_cv2_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Pillow

from PIL import Image

# read image
pil_img = Image.open("test_images/test1.jpg")

# rotate 45 degrees counterclockwise
rotated_pil_img = pil_img.rotate(45)

# show the rotated image
rotated_pil_img.show("pil rotated image")

Syntax

OpenCV

cv2.rotate(src, rotateCode[, dst])

Parameters:

  • src: input array.
  • dst: output array of the same type as src. The size is the same with ROTATE_180, and the rows and cols are switched for ROTATE_90_CLOCKWISE and ROTATE_90_COUNTERCLOCKWISE.
  • rotateCode: an enum to specify how to rotate the array; see the enum RotateFlags.

Returns:

  • An image (Numpy array).
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

Parameters:

  • src: input image.
  • dst: output image that has the size dsize and the same type as src.
  • M: 2×3 transformation matrix.
  • dsize: the size of the output image.
  • flags: a combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dstsrc ).
  • borderMode: pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.
  • borderValue: value used in case of a constant border; by default, it is 0.

Pillow

Image.rotate(angle, resample=Resampling.NEAREST, expand=0, center=None, translate=None, fillcolor=None)

Parameters:

  • angle: In degrees counter-clockwise.
  • resample: An optional resampling filter. This can be one of Resampling.NEAREST (use nearest-neighbour), Resampling.BILINEAR (linear interpolation in a 2×2 environment), or Resampling.BICUBIC (cubic spline interpolation in a 4×4 environment). If omitted, or if the image has mode “1” or “P”, it is set to Resampling.NEAREST. See Filters.
  • expand: Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.
  • center: Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.
  • translate: An optional post-rotate translation (a 2-tuple).
  • fillcolor: An optional color for areas outside the rotated image.

RETURNS:

References

Avatar photo
Steins

Developer & AI Researcher. Write about AI, web dev/hack.