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.


import_contacts

You may also like:

Related Posts

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()Conclusion In this post, we will see how to get year from Date in […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.