Welcome folks today in this blog post we will be rotating images
at any direction in python using opencv library
. All the full source code of the application will be given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install python-opencv
After installing this library make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Python program to explain cv2.rotate() method # importing cv2 import cv2 # path path = r'C:\Users\user\Desktop\geeks14.png' # Reading an image in default mode src = cv2.imread(path) # Window name in which image is displayed window_name = 'Image' # Using cv2.rotate() method # Using cv2.ROTATE_90_CLOCKWISE rotate # by 90 degrees clockwise image = cv2.rotate(src, cv2.cv2.ROTATE_90_CLOCKWISE) # Displaying the image cv2.imshow(window_name, image) cv2.imwrite("outputimage.jpg",image) cv2.waitKey(0) |
As you can see in the above python
script we are rotating the image
in clockwise direction at an angle of 90 degree
you can change the value accordingly as you want. And lastly we are saving the image with the custom name in the root directory and also displaying it also as shown below
If you run the python script by typing the below command
python app.py
And in similar way you can rotate
image in anticlockwise
direction as shown below in the python script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Python program to explain cv2.rotate() method # importing cv2 import cv2 # path path = r'profile.jpg' # Reading an image in default mode src = cv2.imread(path) # Window name in which image is displayed window_name = 'Image' # Using cv2.rotate() method # Using cv2.ROTATE_90_COUNTERCLOCKWISE # rotate by 270 degrees clockwise image = cv2.rotate(src, cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imwrite("anticlockwise.jpg",image) # Displaying the image cv2.imshow(window_name, image) cv2.waitKey(0) |