In this Python Scipy tutorial, we will learn about the “Python Scipy Ndimage Zoom” and cover the following topics.
- Python Scipy Ndimage Zoom
- Python Scipy Ndimage Slow
- Python Scipy Ndimage Order
- Python Scipy Ndimage Mode
- Python Scipy Ndimage Interpolation
- Python Scipy Ndimage example
- Python Scipy Ndimage Interpolation Zoom Nearest
Python Scipy Ndimage Zoom
The Python SciPy has a method zoom()
in a module scipy.ndimage
that uses spline interpolation of the requested order to zoom the array.
The syntax is given below.
scipy.ndimage.zoom(input, zoom, order=2, output=None, mode='wrap', prefilter=False, cval=0.1, grid_mode=True)
Where parameters are:
- input(array_data): The array of inputs.
- zoom(sequence or float): Along the axes, the zoom factor. When using a float, the zoom for each axis is the same. If you’re using a series, zoom should only have one value per axis.
- output(dtype or array): The dtype of the returned array, or the array into which to insert the output. The default is to build an array with the same dtype as input.
- order(int): The spline interpolation order, which is set to 3 by default. The order must fall between 0 and 5.
- mode : The mode argument controls how the input array is expanded outside its bounds. The default value is ‘constant. It supports the following mode.
- grid-mirror
- reflect
- grid-constant
- constant
- wrap
- nearest
- grid-wrap
- mirror
- cval(scalar): If the mode is ‘constant,’ the value is used to fill in the gaps between the input’s edges. 0 is the default value.
- prefilter(boolean): If a spline filter was used to prefilter the input array before interpolation, this value is true. If the order is more than one, the default is True, which creates a temporary float64 array of filtered values. If order > 1, the output will be significantly blurred if this is set to False unless the input is prefiltered, in which case it is the result of running a spline filter on the original input.
- grid-mode(boolean): When False, the space between the pixel centres is magnified. Otherwise, the full pixel extent distance is used. When grid mode is False, a 5d signal is deemed to be 4d, but 5d when grid mode is True.
The method zoom()
returns the zoom
(which is the zoomed input) of type ndarray.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
from matplotlib import image, pyplot
from scipy.ndimage import zoom
Read the image using the method imread()
of the module image
and pass the location of the image to a method.
img = image.imread('lubo-minar.jpg')
View the image using the below code.
pyplot.imshow(img)
Now pass the image to a method zoom()
of module ndimage
to zoom the image using the below code.
zoomed_img = zoom(img,1.5)
In the above code, we have provided the image to a method zoom()
with zoom factor equal to 1.5.
View the zoomed image using the below code.
pyplot.imshow(zoomed_img)
Also check the size of both the image, the original and zoomed one using the below code.
print("Size of the original image",img.shape)
print("Size of the original image",zoomed_img.shape)
This is how to use the method zoom()
of Python SciPy.
Read Python Scipy Pairwise Distance
Python Scipy Ndimage interpolation
The Python SciPy has a method zoom()
in a module scipy.interpolatin
that uses spline interpolation of the requested order to zoom the array. Note that the module scipy.interpolation.zoom
is depreciated, use the module scipy.ndimage.zoom
instead.
The syntax is given below.
scipy.interpolatin.zoom(input, zoom, order=2, output=None, mode='wrap', prefilter=False, cval=0.1, grid_mode=True)
Where parameters are:
- input(array_data): The array of inputs.
- zoom(sequence or float): Along the axes, the zoom factor. When using a float, the zoom for each axis is the same. If you’re using a series, zoom should only have one value per axis.
- output(dtype or array): The dtype of the returned array, or the array into which to insert the output. The default is to build an array with the same dtype as input.
- order(int): The spline interpolation order, which is set to 3 by default. The order must fall between 0 and 5.
- mode : The mode argument controls how the input array is expanded outside its bounds. The default value is ‘constant. It supports the following mode: grid-mirror reflect, grid-constant, constant, wrap, nearest, grid-wrap, and mirror.
- cval(scalar): If the mode is ‘constant,’ the value is used to fill in the gaps between the input’s edges. 0 is the default value.
- prefilter(boolean): If a spline filter was used to prefilter the input array before interpolation, this value is true. If the order is more than one, the default is True, which creates a temporary float64 array of filtered values. If order > 1, the output will be significantly blurred if this is set to False unless the input is prefiltered, in which case it is the result of running a spline filter on the original input.
- grid-mode(boolean): When False, the space between the pixel centres is magnified. Otherwise, the full pixel extent distance is used. When grid mode is False, a 5d signal is deemed to be 4d, but 5d when grid mode is True.
The method zoom()
returns the zoom
(which is the zoomed input) of type ndarray.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
from matplotlib import image, pyplot
from scipy.ndimage.interpolation import zoom
Read the image using the method imread()
of the module image
and pass the location of the image to a method.
img = image.imread('https://i0.wp.com/pythonguides.com/content/khanh-dang.jpg')
View the image using the below code.
pyplot.imshow(img)
Now pass the image to a method zoom()
of module ndimage
to zoom the image using the below code.
img_zoomed = zoom(img,1.5)
In the above code, we have provided the image to a method zoom()
with zoom factor equal to 1.5.
View the zoomed image using the below code.
pyplot.imshow(img_zoomed)
Also check the size of both the image, the original and zoomed one using the below code.
Python Scipy Ndimage example
Here will take another image and zoom that image using the method zoom()
of Python Scipy.
Import the required libraries using the below python code.
from matplotlib import image, pyplot
from scipy.ndimage.interpolation import zoom
Read the image using the method imread()
of the module image
and pass the location of the image to a method.
org_img = image.imread('3452442.jpg')
View the image using the below code.
pyplot.imshow(org_img)
Now provide the image to a method zoom()
of module ndimage
to zoom the image using the below code.
zoomed_pic = zoom(org_img,1.5)
In the above code, we have provided the image to a method zoom()
with a zoom factor equal to 1.5.
View the zoomed image using the below code.
pyplot.imshow(zoomed_pic)
Check the size of both the image, the original and zoomed one using the below code.
print("Size of the original image",org_img.shape)
print("Size of the original image",zoomed_pic.shape)
Python Scipy Ndimage mode
We have already learned about how the method zoom()
works and also learned about the parameters of that method. There is a parameter of a method zoom()
which is mode
that deals with the boundary of the images.
- grid-mirror: This word is used interchangeably with the word “reflect.”
- reflect: By reflecting around the edge of the final pixel, the input is stretched. Half-sample symmetric mode is another name for this mode.
- grid-constant: The input is extended by using the cval argument to fill all values beyond the edge with the same constant value. Interpolation is used for samples that are not inside the scope of the input.
- constant: All values beyond the edge of the input are filled with the same constant value, as defined by the cval argument. Beyond the borders of the input, no interpolation is performed.
- wrap : The input is extended by wrapping around to the opposite edge, but in such a way that the last point and the first point overlap perfectly. In this scenario, the sample that will be chosen at the point of overlap is not well defined.
- nearest : By duplicating the final pixel, the input is enlarged.
- grid-wrap: Wrapping around to the opposite edge extends the input.
- mirror: By reflecting around the centre of the final pixel, the input is stretched.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
from matplotlib import image, pyplot
from scipy.ndimage import zoom
Read the image on which we want to apply the different kinds of modes.
org_img = image.imread('chocolate.jpg')
show the loaded image using the below code.
pyplot.imshow(org_img)
Now provide the image as input to a method zoom()
of the module ndimage
to zoom the image using the below code.
zoomed_pic = zoom(org_img,1.5,mode ='mirror')
In the above code, we have provided the image to a method zoom()
with a zoom factor equal to 1.5 and mode equal to the mirror. Instead of mirror
, we can use the different to get the result according to our needs.
Show the zoomed image using the below code.
pyplot.imshow(zoomed_pic)
Check the size of both the image, the original and zoomed one using the below code.
print("Size of the original image",org_img.shape)
print("Size of the original image",zoomed_pic.shape)
This is how to use the parameter mode to fill the extended part of the images using the different mode values.
Read Python Scipy Ndimage Imread
Python Scipy Ndimage order
While learning about the method zoom()
, we encountered the parameter order
that is spline interpolation order, which is set to 3 by default. The order must fall between 0 and 5. Here we will tune this parameter to see how the image is zoomed when we change the value of the parameter order
.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
Create an array and consider this array containing the values of an image.
arr = [0,1,4,2,3]
Pass the above-created array to a method zoom with an order equal to 2 using the below code.
ndimage.zoom(arr,1.2,order = 2)
Now change the parameter order
value to 5 using the below code.
ndimage.zoom(arr,1.2,order = 5)
From the above output, we can compare the result of both codes that contain the order values equal to 2 and 5. Notice the difference or how the order affected the result.
Read Python Scipy Softmax
Python Scipy Ndimage Interpolation Zoom Nearest
Here in this section, we will use the mode value nearest
in the method zoom()
of Python Scipy. This kind of mode uses the last pixel of the image to fill the extended area of the image. Also, we will use the method zoom()
of module interpolation
of Python Scipy.
To know about the zoom method, refer to the first sub-section of this tutorial.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
Create an array and consider this array containing the values of an image.
arr = [0,1,8,2,6]
Pass the above-created array to a method zoom with default mode equal to nearest using the below code.
ndimage.zoom(arr,1.2,mode ='nearest')
This is how to use the mode ‘nearest’ with the metod zoom()
of Python Scipy to zoom the image pixel.
We have covered the “Python Scipy Ndimage Zoom” and learned how to zoom the image using the different mode values by covering the below topics.
- Python Scipy Ndimage Zoom
- Python Scipy Ndimage Slow
- Python Scipy Ndimage Order
- Python Scipy Ndimage Mode
- Python Scipy Ndimage Interpolation
- Python Scipy Ndimage example
- Python Scipy Ndimage Interpolation Zoom Nearest
You may also like the following Python Scipy tutorials:
- Python Scipy Convolve 2d
- How to use Python Scipy Differential Evolution
- Working with Python Lil_Matrix Scipy
- How to use Python Scipy Linprog
- Python Scipy Sparse Csr_matrix
- How to use Python Scipy Gaussian_Kde
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.