You can rotate an image in OpenCV Python by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.
Open the Spyder IDE (integrated development environment).
Import the OpenCV library. If OpenCV is not installed in your system then first install it using This Method.
import cv2 #cv2 is used for OpenCV library
Now read the image from the location. In my case “F:\\AiHints” is the location and “top30.jpg” is the name of the image. Change it according to your image location and name.
image = cv2.imread("F:\\AiHints\\top30.jpg") #imread is use to read an image from a location
Step 4 (Rotate 90 degrees clockwise)
From this step, You can rotate the image 90 degrees clockwise. (90 degrees clockwise = 270 degrees counter-clockwise). In my case “F:\\AiHints” is the location. Change it according to your location where you want to save the image.
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) cv2.imwrite('F:\\AiHints\\Rotate90Clockwise.jpg',image) cv2.waitKey() #waitKey() open the image for a specific time in milliseconds until you press any key. cv2.destroyAllWindows() #"destroyAllWindows()" will destroy all the windows that we created.
Step 5 (Rotate 90 degrees CounterClockwise)
From this step, You can rotate the image 90 degrees CounterClockwise. (90 degrees counter-clockwise = 270 degrees clockwise)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imwrite('F:\\AiHints\\Rotate90CounterClockwise.jpg',image) cv2.waitKey() cv2.destroyAllWindows()
Step 6 (Rotate 180 degrees)
From this step, You can rotate the image 180 degrees. (180 degrees clockwise = 180 degrees counter-clockwise)
image = cv2.rotate(image, cv2.ROTATE_180) cv2.imwrite('F:\\AiHints\\Rotate180.jpg',image) cv2.waitKey() cv2.destroyAllWindows()