Rotate image in Python

Rotate image Python

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.

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:

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.

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 the Image.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.

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 as np in the code.
  • Next, the Image module is imported from the PIL library.
  • The original image is loaded through the np.array() function along with the Image.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.

The above code provides the same output as that of the method mentioned in the article above.

Explanation
  • The OpenCV and imutils 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 the imutils.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.

The above code provides the same output as all the other methods mentioned above in the article.

Explanation
  • The ndimage and misc modules are imported from the SciPy library.
  • Then, the pyplot module is imported from the matplotlib 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() and show() functions.

That’s all about how to rotate image in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *