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

in a module *zoom()*

that uses spline interpolation of the requested order to zoom the array.*scipy.ndimage*

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:

The array of inputs.*input(array_data):*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.*zoom(sequence or float):*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.*output(dtype or array):*The spline interpolation order, which is set to 3 by default. The order must fall between 0 and 5.*order(int):*The mode argument controls how the input array is expanded outside its bounds. The default value is ‘constant. It supports the following mode.*mode :*- grid-mirror
- reflect
- grid-constant
- constant
- wrap
- nearest
- grid-wrap
- mirror

If the mode is ‘constant,’ the value is used to fill in the gaps between the input’s edges. 0 is the default value.*cval(scalar):*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.*prefilter(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.*grid-mode(boolean):*

The method

returns the *zoom()*(which is the zoomed input) of type ndarray.

`zoom`

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

of the module *imread()*

and pass the location of the image to a method.*image*

`img = image.imread('lubo-minar.jpg')`

View the image using the below code.

`pyplot.imshow(img)`

Now pass the image to a method

of module *zoom()*

to zoom the image using the below code.*ndimage*

`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

in a module *zoom()*

that uses spline interpolation of the requested order to zoom the array. Note that the module *scipy.interpolatin*

is depreciated, use the module *scipy.interpolation.zoom*

instead.*scipy.ndimage.zoom*

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:

The array of inputs.*input(array_data):*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.*zoom(sequence or float):*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.*output(dtype or array):*The spline interpolation order, which is set to 3 by default. The order must fall between 0 and 5.*order(int):*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.*mode :*

If the mode is ‘constant,’ the value is used to fill in the gaps between the input’s edges. 0 is the default value.*cval(scalar):*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.*prefilter(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.*grid-mode(boolean):*

The method

returns the *zoom()*(which is the zoomed input) of type ndarray.

`zoom`

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

of the module *imread()*

and pass the location of the image to a method.*image*

`img = image.imread('/content/khanh-dang.jpg')`

View the image using the below code.

`pyplot.imshow(img)`

Now pass the image to a method

of module *zoom()*

to zoom the image using the below code.*ndimage*

`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** ** of Python Scipy.

`zoom()`

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

of the module *imread()*

and pass the location of the image to a method.*image*

`org_img = image.imread('3452442.jpg')`

View the image using the below code.

`pyplot.imshow(org_img)`

Now provide the image to a method

of module *zoom()*

to zoom the image using the below code.*ndimage*

`zoomed_pic = zoom(org_img,1.5)`

In the above code, we have provided the image to a method

with a zoom factor equal to 1.5.*zoom()*

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

which is *zoom()*`mode`

that deals with the boundary of the images.

This word is used interchangeably with the word “reflect.”*grid-mirror:*- reflect: By reflecting around the edge of the final pixel, the input is stretched. Half-sample symmetric mode is another name for this mode.
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.*grid-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.*constant:*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.*wrap :*By duplicating the final pixel, the input is enlarged.*nearest :*Wrapping around to the opposite edge extends the input.*grid-wrap:*By reflecting around the centre of the final pixel, the input is stretched.*mirror:*

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

of the module *zoom()*

to zoom the image using the below code.*ndimage*

`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

, we encountered the parameter *zoom()*`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

in the method *nearest*

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 *zoom()*

of Python Scipy. *interpolation*

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

of Python Scipy to zoom the image pixel.*zoom()*

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

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.