Scipy Ndimage Rotate

In this Python tutorial, we will learn about “Scipy ndimage rotate” where we will rotate the image at different angles with server parameters like axis, order, etc. And cover the following topics.

  • Scipy ndimage rotate
  • Scipy ndimage rotate order
  • Scipy ndimage shift example
  • Scipy ndimage rotate interpolation rotate order
  • Scipy ndimage rotate interpolation rotate axis

Also, check the latest tutorial on Python SciPy: Python Scipy Derivative of Array

Scipy ndimage rotate

The Python Scipy has a module scipy.ndimage to manipulate images or perform an image processing operation and this module has several methods to deal with image processing tasks. Here we will use one of the methods rotate() to rotate the given image at a specified angle.

The syntax of the method scipy.ndimage.rotate() is given below.

scipy.ndimage.rotate(input, angle, axes=(1, 0), reshape=True, output=None, mode='constant')

Here are the most common parameters that are used with the method roatate().

  • input(array_data): It is the input array or image that we want to rotate.
  • angle(float): It is used to specify the rotation angle like 20 degrees or 90 degrees.
  • axes(two ints in tuples): The plane of rotation is defined by the two axes.
  • output(datatype or array): It defines the datatype of the returned array, by default returned array datatype is the same as the input array.
  • mode: It is used to specify the modes ‘constant’, ‘mirror’, ‘wrap’, ‘reflect’, ‘nearest’.

Let’s understand through an example by following the below steps:

Import the required libraries such matplotlib and scipy using the below code.

from matplotlib import image, pyplot
from scipy import ndimage

Load the image from the directory or wherever the image exists on your computer using the below code.

img = image.imread('luca_florio.jpg')

Here the function imread()of the module image read the images in the form of an array of pixels.

View the loaded image using the function imshow() of the module pyplot as shown below.

pyplot.imshow(img)
Scipy ndimage rotate example
Scipy ndimage rotate example

Let’s rotate the above-loaded image to some angle using the below code.

luca_florio_rotate = ndimage.rotate(img,120,mode='constant')

Here in the above code, we are using the function rotate() of the module ndimage of Python Scipy to rotate the image. Also providing some parameters like the image img that we want to rotate, the angle of the rotation of 120 degrees, and the mode as constant.

Now view the image which rotated to an angle of 120 degrees using the below code.

pyplot.imshow(luca_florio_rotate)
Scipy ndimage rotate
Scipy ndimage rotate

Look in the above output at how we have rotated the image to an angle of 120 degrees.

Read: Scipy Convolve

Scipy ndimage rotate order

Here we will use the same method rotate() to rotate the given image at a specified angle that we have learned in the above sub-section.

The syntax of the method scipy.ndimage.rotate() is given below.

scipy.ndimage.rotate(input, angle, axes=(1, 0), reshape=True, output=None, mode='constant')

Here are the most common parameters that are used with the method roatate().

  • input(array_data): It is the input array or image that we want to rotate.
  • angle(float): It is used to specify the rotation angle like 20 degrees or 90 degrees.
  • axes(two ints in tuples): The plane of rotation is defined by the two axes.
  • output(datatype or array): It defines the datatype of the returned array, by default returned array datatype is the same as the input array.
  • mode: It is used to specify the modes ‘constant’, ‘mirror’, ‘wrap’, ‘reflect’, ‘nearest’.

Let’s understand through an example by following the below steps:

Import the required libraries such matplotlib and scipy using the below code.

from matplotlib import image, pyplot
from scipy import ndimage

Load the image from the directory or wherever the image exists on your computer using the below code.

img = image.imread('luca_florio.jpg')

Here the function imread()of the module image read the images in the form of an array of pixels.

View the loaded image using the function imshow() of the module pyplot as shown below.

pyplot.imshow(img)
Scipy ndimage rotate order example
Scipy ndimage rotate order example

Rotate the image on the default order parameter value using the below code.

mykhailo_rotate = ndimage.rotate(img,100,mode='constant')
pyplot.imshow(mykhailo_rotate)

Again rotate the same image by specifying the order parameter value as 4 using the below code.

mykhailo_rotate = ndimage.rotate(img,100,mode='constant',order= 4)
pyplot.imshow(mykhailo_rotate)
Scipy ndimage rotate order
Scipy ndimage rotate order

This is how to rotate an image by specifying the order parameters of the method rotate().

Read: Scipy Constants

Scipy ndimage shift example

The Python Scipy submodule scipy.ndimage has a method shift() to shift the array of images using the spline interpolation.

The syntax is given below.

scipy.ndimage.shift(input, shift, output=None mode='constant')

Where parameters are:

  • input(array_data): It is the input array or image that we want to shift.
  • shift(sequence or float): It is used to specify the position(axis) for shifting the image.
  • output(datatype or array): It defines the datatype of the returned array, by default returned array datatype is the same as the input array.
  • mode: It is used to specify the modes ‘constant’, ‘mirror’, ‘wrap’, ‘reflect’, ‘nearest’.

Let’s understand how to shift the image by following the below steps:

Import the required libraries such matplotlib and scipy using the below code.

from matplotlib import image, pyplot
from scipy import ndimage

Load the image from the directory or wherever the image exists on your computer using the below code.

img = image.imread('thehouse.jpg')
pyplot.imshow(img)
Scipy ndimage shift example
Scipy ndimage shift example

Let’s shift the image to a different position using the method shift().

thehouse_shift = ndimage.shift(img,(90,90,0),mode='nearest')

Here in the above code, we are using the function shift() of the module ndimage of Python Scipy to shift the image. Also providing some parameters like the image img that we want to shift, the x and y position of the for shifting the image with the color channel as (90,90,0) , and the mode as nearest.

Now view the shifted image using the below code.

pyplot.imshow(thehouse_shift)
Scipy ndimage shift
Scipy ndimage shift

Read: Scipy Optimize – Helpful Guide

Scipy ndimage rotate interpolation rotate order

The Python Scipy has a method rotate() within the module scipy.ndimage.interpolation to rotate an image at specified degrees. It rotates the images using the spline interpolation algorithms, the spline interpolation algorithms help in image resizing or distortion.

Here we will see the effect on the image or provided array by changing the order of the spline interpolation.

The syntax is given below.

scipy.ndimage.interpolation.rotate(input, axes=(1, 0), angle, output=None, reshape=True, order=2, cval=0.0, mode='constant', prefilter=True)

Where parameters are:

  • input(array_data): It is the input array or image that we want to rotate.
  • angle(float): It is used to specify the rotation angle like 20 degrees or 90 degrees.
  • reshape(boolean): If it is true, then the shape is adjusted according to the input array, otherwise, in the case of false it doesn’t adapt according to the input array.
  • axes(two ints in tuples): The plane of rotation is defined by the two axes.
  • order(int): It is used to specify spline interpolation order.
  • output(datatype or array): It defines the datatype of the returned array, by default returned array datatype is the same as the input array.
  • cval(scalar): If the mode is set to constant, then these values are used at the outside boundaries of the provided input array.
  • mode: It is used to specify the modes ‘constant’, ‘mirror’, ‘wrap’, ‘reflect’, ‘nearest’.

The method rotate() returns the rotated ndarray of the input array.

Here we will take the example by following the below steps:

Import the required libraries such matplotlib and scipy using the below code.

from matplotlib import image, pyplot
from scipy import ndimage

Load the image from the directory or wherever the image exists on your computer using the below code.

img = image.imread('/content/kevin.jpg')

Here the function imread()of the module image read the images in the form of an array of pixels.

View the loaded image using the function imshow() of the module pyplot as shown below.

pyplot.imshow(img)
Scipy ndimage rotate interpolation rotate order example
Scipy ndimage rotate interpolation rotate order example

First, rotate the image with the default order which is 3 using the below code.

kevin_rotate_order = rotate(img,60,mode='constant')
pyplot.imshow(kevin_rotate_order)

Now provide a new value as 5 to a parameter order and see the effect using the below code.

kevin_rotate_ = rotate(img,60,mode='constant',order=5)
pyplot.imshow(kevin_rotate_)
Scipy ndimage rotate interpolation rotate order
Scipy ndimage rotate interpolation rotate order

Looking at both images, we have noticed a little change in image rotation and size.

Read: Scipy Stats – Complete Guide

Scipy ndimage rotate interpolation rotate axis

Here we will use the Python Scipy same method rotate() of module scipy.ndimage.interpolation to rotate an image at specified degrees that we have learned in the above sub-section. So here we will rotate the image by changing the axis parameter of that method.

Import the required libraries matplotlib and scipy by using the below code.

from matplotlib import image, pyplot
from scipy import ndimage

Load the image from the directory or wherever the image exists on your computer using the below code.

img = image.imread('/content/kevin.jpg')

Here the function imread()of the module image read the images in the form of an array of pixels.

View the loaded image using the function imshow() of the module pyplot as shown below.

pyplot.imshow(img)
Scipy ndimage rotate interpolation rotate axis example
Scipy ndimage rotate interpolation rotate axis example

Rotate the above image to some angle with default axes using the below code.

annie_rotate_axis = rotate(img,70,mode='constant')
pyplot.imshow(annie_rotate_axis)

Now rotate the same image to some angle by changing the axes using the below code.

annie_rotate_ = rotate(img,70,mode='constant',axes=(0,1))
pyplot.imshow(annie_rotate_)
Scipy ndimage rotate interpolation rotate axis
Scipy ndimage rotate interpolation rotate axis

This is how to rotate the image by changing the axes parameters.

Also, take a look at some more Python Scipy tutorials.

So, in this tutorial, we have learned about the “Scipy ndimage rotate” and covered the following topics.

  • Scipy ndimage rotate
  • Scipy ndimage rotate order
  • Scipy ndimage shift example
  • Scipy ndimage rotate interpolation rotate order
  • Scipy ndimage rotate interpolation rotate axis