Scipy Misc + Examples

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

Scipy Misc

In Scipy there is a module scipy.misc that is Miscellaneous routines which has different utilities or methods that don’t belong to any specific module.

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

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

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

Also, check: Scipy Constants

Scipy Misc imread

The Scipy method imread() 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 imageio. So here we will install this library and read an image.

The syntax is given below.

imageio.imread(file_path)

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

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 imread() that reads the image from a specified path and returns the ndarray of that image.

Now view the image using the method imshow() of submodule matplotlib.pyplot using the below code.

plt.figure(figsize=(5,5))
plt.imshow(image) 
plt.axis('off')
plt.show()
Scipy Misc imread
Scipy Misc imread

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

Read: Scipy Optimize

Scipy Misc imsave

The Scipy method imsave() which is used to save an image from an array to a file was removed from Scipy version 1.2.0. The method imwrite() that exists in another library imageio is used in place of that method.

READ:  Matplotlib fill_between - Complete Guide

The syntax is given below.

imageio.imwrite(uri,array_data,format)

Where the parameters are:

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

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)
Scipy Misc imsave
Scipy Misc imsave

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

Read: Scipy Sparse

Scipy Misc toimage

The Scipy method toread() which is used to create an image from an array was removed from Scipy version 1.2.0. The method PIL.mage.fromarray() that exists in another library PIL is used in place of that method.

The syntax is given below.

image.fromarray(obj)

Where parameters are:

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

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()
Scipy Misc toimage
Scipy Misc toimage

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

Read: Scipy Rotate Image

Scipy Misc derivative

The Scipy module misc contains a method derivative() that finds the nth derivative of a given function at a point.

The syntax is given below.

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

Where parameters are:

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

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)
Scipy Misc derivative
Scipy Misc derivative

Read: Scipy Integrate + Examples

Scipy Misc imresize

The method imresize() from the module scipy.misc is deprecated in the Scipy version 1.0. Here instead of a method imresize(), we can use the method resize() of library pillow.

The syntax is given below.

image.resize(size)

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

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

Import the module image of the library pillow using the below code.

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
Scipy Misc imresize
Scipy Misc imresize

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
Scipy Misc imresize example
Scipy Misc imresize example

Read: Scipy Signal – Helpful Tutorial

Scipy Misc comb

The method comb() from the module scipy.misc is deprecated in the Scipy version 1.0. The method has moved into another module named scipy.special. This method is used for finding the number of combinations of n things taken at k time.

The syntax is given below.

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

Where parameters are:

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

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)
Scipy Misc comb
Scipy Misc comb

Read: Scipy Convolve – Complete Guide

Scipy Misc ascent

The Scipy module misc contains another method ascent() to get the 8-bit grayscale bit depth of a derived image of size 512×512.

READ:  Python Tkinter Entry - How to use

The syntax is given below.

scipy.misc.ascent()

The method returns the ascent type ndarray image for demonstration purposes.

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 ascent
Scipy Misc ascent

Scipy Misc face

The Scipy module misc contains another method 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 face
Scipy Misc face

Scipy Misc factorial

The method factorial() from the module scipy.misc is deprecated in the Scipy version 1.0. The method has moved into another module named scipy.special. This method is used to find the factorial of a number or all the elements contained within an array.

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 factorial() using the below.

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

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