In this Python tutorial, we will learn about the “**Scipy Misc**” and discuss multiple examples related to it. Additionally, we will cover the following topics.

- Scipy Misc
- Scipy Misc imread
- Scipy Misc imsave
- Scipy Misc toimage
- Scipy Misc derivative
- Scipy Misc imresize
- Scipy Misc comb
- Scipy Misc ascent
- Scipy Misc face
- Scipy Misc factorial

**Table of Contents**show

## Scipy Misc

In Scipy there is a module

that is *scipy.misc*

which has different utilities or methods that don’t belong to any specific module.*Miscellaneous routines*

It has five methods for five different purposes which are given below.

It finds the weights for an Np-point of a given central derivative.*central_diff_weights():*It returns the raccoon face with a size of 1024 x 768.*face():*It returns the 8-bit grayscale bit-depth of size 512 x 512 which is a derived image for demo purposes.*ascent():*It is used to load an electrocardiogram as the 1-D signal.*electrocardiogram():*It returns the nth derivative of a given function at a point.*derivative():*

Later in this tutorial, we will learn about the above methods separately.

Also, check: Scipy Constants

## Scipy Misc imread

The Scipy method

which is used to read an image from a file is removed from Scipy version 1.2.0. The method exists in another library that is *imread()*

. So here we will install this library and read an image.*imageio*

The syntax is given below.

`imageio.imread(file_path)`

Where the

is the path of the image that we want to read.*file_path*

Open a new Jupiter notebook or install the library using the cmd on your computer as shown below steps:

`!pip install imageio`

Import the libraries using the below code.

```
import imageio
import matplotlib.pyplot as plt
%matplotlib inline
```

Read the image using the method `imread()`

.

`image = imageio.imread('keith_tanner.jpg')`

The above code contains the method

that reads the image from a specified path and returns the ndarray of that image.*imread()*

Now view the image using the method

of submodule *imshow()*

using the below code.*matplotlib.pyplot*

```
plt.figure(figsize=(5,5))
plt.imshow(image)
plt.axis('off')
plt.show()
```

This how-to read the image using the method `imread`

.

Read: Scipy Optimize

## Scipy Misc imsave

The Scipy method

which is used to save an image from an array to a file was removed from Scipy version 1.2.0. The method *imsave()*`imwrite()`

that exists in another library

is used in place of that method.*imageio*

The syntax is given below.

`imageio.imwrite(uri,array_data,format)`

Where the parameters are:

It is the name of the file that we want to save.*uri(string):*It is the array values of an image.*array_data:*It is used to specify the format of the image that we want to read.*format(string):*

Let’s take an example by following the below steps:

Import the required libraries using the below code.

```
import numpy as np
import imageio
```

Creating rows and columns using tuples as shown in the below code.

`row, col = (10,10)`

Creating an array using the defined row and col.

`array_data = np.zeros((row,col))`

Create an image using the below code.

```
image = imageio.imwrite('creating_img.png',array_data)
```

The above code creates the image of 64 bytes with zeros values.

Read: Scipy Sparse

## Scipy Misc toimage

The Scipy method

which is used to create an image from an array was removed from Scipy version 1.2.0. The method *toread()*

that exists in another library *PIL.mage.fromarray()*

is used in place of that method.*PIL*

The syntax is given below.

`image.fromarray(obj)`

Where parameters are:

It is an array containing the image values.*obj:*

Let’s understand by an example using the below steps:

First, install the library `PIL`

as here we are going to install the library in Jupyter Notebook.

`!pip install Pillow`

Import the required libraries using the below code.

```
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Open any image and convert it into an array.

```
img = Image.open("severin_candrian.jpg")
array = np.array(img)
```

Create a PIL image from the above array using the below code.

`pilimage=Image.fromarray(array)`

View the image using the below code.

```
plt.figure(figsize=(5,5))
plt.imshow(pilimage)
plt.axis('off')
plt.show()
```

This is how to creates an image from a given array.

Read: Scipy Rotate Image

## Scipy Misc derivative

The Scipy module

contains a method *misc*

that finds the nth derivative of a given function at a point.*derivative()*

The syntax is given below.

`scipy.misc.derivative(func, x0, dx=1.0, n=1, args=(), order=3)`

Where parameters are:

It is the function whose nth derivative we want to find.*func:*It is used to specify the point where the derivative is found.*x0(float):*It is used to specify the spacing.*dx(float):*It is used to specify the order of the derivative.*n(int):*It is used to provide the argument.*args(tuple):*It is used to specify the number of points to use.*order(int):*

Let’s take an example using the below code.

```
from scipy.misc import derivative
def fun(x):
return x**4 + x**3
derivative(fun, 1.0, dx=1e-7)
```

Read: Scipy Integrate + Examples

## Scipy Misc imresize

The method

from the module *imresize()*

is deprecated in the Scipy version 1.0. Here instead of a method *scipy.misc*

, we can use the method *imresize()*

of library pillow.*resize()*

The syntax is given below.

`image.resize(size)`

Where parameter

takes the pixel size of the image whose size we want to resize.*size*

Let’s take an example by following the below steps:

Import the module

of the library *image*

using the below code.*pillow*

```
import PIL.Image as img
import matplotlib.pyplot as plt
```

Open any image that we want to resize using the below code.

`imz = img.open('lesly_derksen.jpg')`

View and check the size of an image using the below code.

```
plt.imshow(imz)
imz.size
```

Define the new size for the image as shown in the below code.

`new_size = (300,300)`

Let’s reduce the size of the image using the below code.

`imz1= imz.resize(new_size)`

Now view the reduced size of the image using the below code.

```
plt.imshow(imz1)
imz1.size
```

Read: Scipy Signal – Helpful Tutorial

## Scipy Misc comb

The method

from the module *comb()*

is deprecated in the Scipy version 1.0. The method has moved into another module named *scipy.misc*

. This method is used for finding the number of combinations of n things taken at k time.*scipy.special*

The syntax is given below.

`scipy.special.comb(N, k, exact=False, repetition=False)`

Where parameters are:

It is used to specify the number of things.*N(ndarray or int):*It is used to specify the number of elements taken.*K(ndarray or int):*It computes the right solution using the long integer arithmetic when it is True, otherwise computes the approximated solution in floating-point.*exact(boolean):*If it is true, then it calculates the number of combinations with repetition.*repetition(bool):*

Let’s take an example using the below code.

```
from scipy.special import comb
k_time = np.array([6, 7])
n_things = np.array([15, 15])
comb(n_things, k_time, exact=False)
```

Read: Scipy Convolve – Complete Guide

## Scipy Misc ascent

The Scipy module** ** contains another method

`misc`

*ascent()*

to get the 8-bit grayscale bit depth of a derived image of size 512×512.The syntax is given below.

`scipy.misc.ascent()`

The method returns the

type ndarray image for demonstration purposes.*ascent*

Let’s take an example using the below code.

```
import scipy.misc as f
import matplotlib.pyplot as plt
asc = f.ascent()
plt.imshow(asc)
```

## Scipy Misc face

The Scipy module** ** contains another method

`misc`

`face()`

to get the colorful raccoon face of size 1024×768.The syntax is given below.

`scipy.misc.face(gray=False)`

Where a parameter is:

** gray(boolean):** It returns the 8-bit grayscale image when it is True, otherwise returns the colourful image.

Let’s take an example by following the below steps.

Import the necessary files.

```
import scipy.misc as f
import matplotlib.pyplot as plt
```

Generate the racoon face using the below code.

`racoon_face = f.face()`

show the generated image using the below code.

`plt.imshow(racoon_face)`

## Scipy Misc factorial

The method

from the module *factorial()*

is deprecated in the Scipy version 1.0. The method has moved into another module named *scipy.misc*

. This method is used to find the factorial of a number or all the elements contained within an array.*scipy.special*

The syntax is given below.

`scipy.special.factorial(n, exact=False)`

Where parameters are:

** n(int or array):** It is the number or array of numbers whose factorial we want to find.

** exact(boolean):** It computes the right solution using the long integer arithmetic when it is True, otherwise computes the approximated solution in floating-point.

Let’s take an example and calculate the factorial of all the elements with an array using the below steps.

Import the required libraries using the below code.

```
from scipy.special import factorial
import numpy as np
```

create an array and pass it to the method

using the below.*factorial()*

```
array = np.array([5, 6, 7])
factorial(array, exact=False)
```

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

- Scipy Misc
- Scipy Misc imread
- Scipy Misc imsave
- Scipy Misc toimage
- Scipy Misc derivative
- Scipy Misc imresize
- Scipy Misc comb
- Scipy Misc ascent
- Scipy Misc face
- Scipy Misc factorial

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.