It is easy to rotate images in opencv. In this tutorial, we will introduce how to use cv2.rotate() to implement it.
Meanwhile, we also can use cv2.warpAffine() to rotate images.
Rotating an Image Using cv2.warpAffine() in Python OpenCV
1.Open an image
import cv2 img = cv2.imread('data/src/lena.jpg') print(type(img)) # <class 'numpy.ndarray'> print(img.shape) # (225, 400, 3)
2.Rotate images with
- cv2.ROTATE_90_CLOCKWISE
- cv2.ROTATE_90_COUNTERCLOCKWISE
- cv2.ROTATE_180
img_rotate_90_clockwise = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) cv2.imwrite('data/dst/lena_cv_rotate_90_clockwise.jpg', img_rotate_90_clockwise) # True img_rotate_90_counterclockwise = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imwrite('data/dst/lena_cv_rotate_90_counterclockwise.jpg', img_rotate_90_counterclockwise) # True img_rotate_180 = cv2.rotate(img, cv2.ROTATE_180) cv2.imwrite('data/dst/lena_cv_rotate_180.jpg', img_rotate_180) # True
Run this code, you will see three rotated images: