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.

Related Posts

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Auto clicker in Python
    30 August

    How to make auto clicker in Python

    Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write variable to file in Python
    30 August

    Write variable to file in python

    Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

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.