Rotate the Image in Python using OpenCV¶
In [2]:
import cv2
img = cv2.imread('machinelearning.png',1)
cv2.imshow('Machine Learning',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Rotating the Image¶
There are a lot of steps involved in rotating an image. The 2 main functions used here are –
- getRotationMatrix2D()
- warpAffine()
getRotationMatrix2D() : It takes 3 arguments
- center – The center coordinates of the image
- Angle – The angle (in degrees) by which the image should be rotated
- Scale – The scaling factor
warpAffine()
- The function warpAffine transforms the source image using the rotation matrix:
In [3]:
import cv2
img = cv2.imread('machinelearning.png',1)
h,w = img.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, 45, 0.5)
rotated = cv2.warpAffine(img, matrix, (w, h))
cv2.imshow('Machine Learning',rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()