Python supports the use of images and sometimes a given image needs to be rotated to a certain angle. The image in Python is rotated about the center and with a certain angle that is specified. Image Rotation is a geometric transformation, and this process can be carried out by Inverse or Forward Transformation.
This tutorial discusses the different ways available to rotate image in Python.
Table of Contents
Using the image.rotate()
function from the Pillow Imaging Library (Fork) to rotate image in Python.
The Pillow Imaging Library (PIL) is an open-source library that can be additionally inserted into the Python code and can be utilized for providing the functions that assist in the opening, saving, and manipulating of different image file formats in Python. The Pillow library is available to use on Windows
, Linux
, and MacOS X
.
The rotate()
function of the Pillow Imaging Library is utilized to rotate an image by a specified angle. The rotation is carried out in a counter-clockwise direction. The rotate()
function successfully implements the task of rotating an image through Inverse Transformation.
The syntax for the rotate()
function is mentioned below:
1 2 3 4 |
from PIL import Image image.rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None) |
The rotate()
function consists of a lot of parameters, but most of them are optional and can be read about on the internet. The only parameter which is mandatory to mention is the angle
parameter. This specifies the angle in degrees at which the image needs to be rotated (In a Counter-clockwise direction). In this method, the process is usually carried out perfectly when the angle mentioned by the user is in multiples of 90.
The following code uses the image.rotate()
function from the Pillow Imaging Library (Fork) to rotate an image to 90 degrees in Python.
1 2 3 4 5 6 7 8 |
from PIL import Image orig = Image.open("gojo.jpg") orig.show() angle = 90 rotated = orig.rotate(angle) rotated.show() |
The above code provides the following output (Images):
Original Image:
Rotated Image (Counter-clockwise 90 degrees):
Explanation
- The
Image
module is imported from the PIL library to the Python code. - The original image is then opened and stored as
orig
in the Python code using theImage.open()
function. - The original image is printed by using the
show()
function. - Then, a variable is taken and an angle is specified by the programmer. This is the angle at which the counter-clockwise rotation process will occur.
- Then, the
rotate()
function is applied to the original image and the angle variable is taken as an argument to the function. - Finally, the rotated image is printed with the help of the
show()
function.
Using NumPy
library functions to rotate image in Python.
The term NumPy
is an abbreviation for Numerical Python, which is a library provided in Python that is mainly utilized in dealing with matrices and arrays in the given Python code. To successfully implement rotation of an image in Python using this method, both the NumPy
and PIL
library need to be imported to the code first.
We use the np.array()
function to take the image as an array first, and then use the np.rot90()
function to rotate the image in the multiples of 90 degrees.
The function also contains a second optional argument that specifies the number which would be multiplied to 90 to get the angle at which the rotation needs to take place. For example, if the second argument is passed as 3, then the angle at which the rotation would take place would be three times 90 degrees, which is 270 degrees.
The following code uses the NumPy
library functions to rotate image in Python.
1 2 3 4 5 6 |
import numpy as np from PIL import Image orig = np.array(Image.open('gojo.jpg')) Image.fromarray(np.rot90(input_image)).save('gojo1.jpg') |
The above code will return an image rotated by 90 degrees, much like the output of the last method mentioned above in the article.
Explanation
- The
NumPy
module is imported asnp
in the code. - Next, the
Image
module is imported from thePIL
library. - The original image is loaded through the
np.array()
function along with theImage.open()
function. - Then, the
np.rot90()
function is applied to the original image. - The rotated image is displayed in the output section.
Using the OpenCV
and imutils
library functions to rotate image in Python.
OpenCV is a gigantic library that focuses on providing several functions which are utilized for Machine learning, Computer Vision, and Image Processing in Python. Apart from being used in Python, the library is also supported by other languages like Java, C++, etc.
The imutils
module is an image-processing library that assists with everything related to images. It needs to be imported along with the OpenCV
library in order to successfully implement the rotation process using this method.
The following code uses the OpenCV
and imutils
library functions to rotate image in Python.
1 2 3 4 5 6 7 8 |
import cv2 import imutils orig = cv2.imread(r".\gojo.jpg") rotated = imutils.rotate(orig, angle=90) cv2.imshow("Rotated image", rotated) cv2.waitKey(0) |
The above code provides the same output as that of the method mentioned in the article above.
Explanation
- The
OpenCV
andimutils
modules need to be imported to the Python code. - The original image is read and loaded onto the code with the help of the
cv2.imread()
function. - Then, the
imutils
module is utilized and theimutils.rotate()
function is used to carry out the rotation process on the original image. - Finally, the output of the rotated image is made to be displayed on the screen.
- Note : The
cv2.waitKey()
is utilized to display something on the screen until and unless the user presses any key.
Using matplotlib
and SciPy
libraries to rotate image in Python.
Matplotlib
is a cross-platform library that is mainly used for graphical plotting and data visualization in Python and the NumPy
library which is an extension of Python. SciPy is an abbreviation for Scientific Python and it is a free-to-use and open-source scientific computation library that majorly uses and works on the NumPy
library.
Both of these libraries are essential and need to be imported in order to rotate an image using this method.
The following code uses the matplotlib
and SciPy
to rotate image in Python.
1 2 3 4 5 6 7 8 9 |
from scipy import ndimage, misc from matplotlib import pyplot as plt orig = misc.face() rotated = ndimage.rotate(orig, 90, mode = 'mirror') plt.imshow(rotated) plt.show() |
The above code provides the same output as all the other methods mentioned above in the article.
Explanation
- The
ndimage
andmisc
modules are imported from theSciPy
library. - Then, the
pyplot
module is imported from thematplotlib
library. - The
misc.face()
function is then applied to load the original image. - Then, the
ndimage.rotate()
function is utilized to rotate the given image. - Finally, the rotated image is displayed in the output by using the
imshow()
andshow()
functions.
That’s all about how to rotate image in Python.