Importing the necessary libraries
In order to rotate an image using Python, we need to import the necessary libraries. The main library we will be using is the OpenCV library, which provides various functions for image processing. To import the OpenCV library, we can use the following code:
«`python
import cv2
«`
In addition to the OpenCV library, we also need to import the NumPy library, which provides support for large, multi-dimensional arrays and matrices. We will use NumPy to manipulate the image data. To import the NumPy library, we can use the following code:
«`python
import numpy as np
«`
Loading and displaying the image
Before we can rotate an image, we need to load the image into our program. To do this, we can use the `imread()` function from the OpenCV library. The `imread()` function takes the path to the image file as input and returns a NumPy array representing the image.
Here is an example of how to load an image:
«`python
image = cv2.imread(‘image.jpg’)
«`
Once we have loaded the image, we can display it using the `imshow()` function from the OpenCV library. The `imshow()` function takes the window name and the image as input and displays the image in a new window.
Here is an example of how to display the image:
«`python
cv2.imshow(‘Original Image’, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
«`
Defining the rotation function
Now that we have loaded and displayed the image, we can define a function to rotate the image. The function will take the image and the angle of rotation as input and return the rotated image.
Here is an example of how to define the rotation function:
«`python
def rotate_image(image, angle):
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
return rotated_image
«`
In this function, we first get the height and width of the image using the `shape` attribute of the image array. We then use the `getRotationMatrix2D()` function from the OpenCV library to create a rotation matrix. The rotation matrix is defined by the center of rotation (which is the center of the image), the angle of rotation, and the scale factor (which is set to 1 in this case).
Once we have the rotation matrix, we can use the `warpAffine()` function from the OpenCV library to apply the rotation to the image. The `warpAffine()` function takes the image, the rotation matrix, and the size of the output image as input, and returns the rotated image.
Rotating the image
Now that we have defined the rotation function, we can use it to rotate the image. To do this, we simply call the rotation function and pass in the image and the desired angle of rotation.
Here is an example of how to rotate the image:
«`python
angle = 45
rotated_image = rotate_image(image, angle)
«`
In this example, we are rotating the image by 45 degrees. You can change the angle to any value you like.
Displaying the rotated image
Once we have rotated the image, we can display the rotated image using the `imshow()` function from the OpenCV library, just like we did with the original image.
Here is an example of how to display the rotated image:
«`python
cv2.imshow(‘Rotated Image’, rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
«`
Complete code
Here is the complete code for rotating an image using Python:
«`python
import cv2
import numpy as np
def rotate_image(image, angle):
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
return rotated_image
image = cv2.imread(‘image.jpg’)
cv2.imshow(‘Original Image’, image)
angle = 45
rotated_image = rotate_image(image, angle)
cv2.imshow(‘Rotated Image’, rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
«`
Make sure to replace `’image.jpg’` with the path to your own image file.
That’s it! You now have a Python program that can rotate an image. Feel free to experiment with different angles and images to see the results.