Python Scipy Ndimage Imread Tutorial

We will learn about the “Python Scipy Ndimage Imread” to read the image as an array and flatten and change the mode of the image with the following topics.

  • Python Scipy Ndimage Imread
  • Python Scipy Ndimage Imread Deprecated
  • How to use the Scipy Ndimage Imread to Flatten the image
  • How to change the mode of the image

Python Scipy Ndimage Imread

To read an image Python Scipy has a method imread() in module scipy.ndimage. In other words, The method imread() of Python Scipy read an image as an array from a file.

The syntax is given below.

scipy.ndimage.imread(fname, flatten=False, mode=None)

Where parameters are:

  • fname(string, file object): The file name or file object that has to be read.
  • flatten(boolean): Flattens the color layers into a single layer of grayscale, if True.
  • mode(string): Image conversion mode, such as “RGB.”

The method returns imread(which is the array retrieved from image reading) of type ndarray.

We will take an image and read it using the method imread() by following the below steps:

Import the required libraries or methods using the python code.

from scipy.ndimage import imread
import matplotlib.pyplot as plt
%matplotlib inline

Read the image as an array form using the below code.

read_img = imread('https://i0.wp.com/pythonguides.com/content/assortment.jpg')

Check the returned values from the method imread() using the below code.

read_img[0]

Now view the read image as an array using the method imshow() matplotlib using the below code.

plt.imshow(read_img)
Python Scipy Ndimage Imread
Python Scipy Ndimage Imread

This is how to read the image as an array form using the method imread() of Python Scipy.

Read: Python Scipy Softmax

Python Scipy Ndimage Imread Deprecated

The method imread() is deprecated in Scipy version = 1.0.0 or doesn’t exist in the latest stable version of Scipy == 1.9.0. The Scipy community suggests the use matplotlib.pyplot.imread instead of scipy.ndimage.imread. The old version of Scipy = 1.2.0 is used in the above subsections for demonstration purposes.

In this section, we will use the method imread() from the module matplotlib.pyplot to read the image.

Import the required libraries or methods using the python code.

import matplotlib.pyplot as plt
%matplotlib inline

Read the image as an array form using the below code.

read_img = plt.imread('https://i0.wp.com/pythonguides.com/content/wide-angle-.jpg')

Check the returned array values from the method plt.imread() using the below code.

read_img[1]

Now view the read image as an array using the method imshow() matplotlib using the below code.

plt.imshow(read_img)
Python Scipy Ndimage Imread Deprecated
Python Scipy Ndimage Imread Deprecated

This is how to read an image as an array using the method imread() of Python Matplotlib.

Read: Working with Python Lil_Matrix Scipy

Python Scipy Ndimage Imread Flatten

The method imread() of Python Scipy accepts a parameter flatten that flattens the color layers into a single layer of grayscale if True. To feed information from a 1-D array into a classification model, a technique known as flattening is utilized to transform multidimensional arrays into 1-D arrays.

The main reason behind flattening the image array before feeding the data to our model is because multi-dimensional arrays use more memory whereas one-dimensional arrays use less memory.

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

Import the required libraries or methods using the python code.

import matplotlib.pyplot as plt
%matplotlib inline

Read the image of the USA White House image as an array form and check the returned array values using the code below.

read_img = imread('https://i0.wp.com/pythonguides.com/content/WhiteHouse.jpg')
read_img[0]

View the read image as an array using the method imshow() matplotlib using the below code.

plt.imshow(read_img)
Python Scipy Ndimage Imread Flatten Example
Python Scipy Ndimage Imread Flatten Example

Flatten the image of the USA White House using the below code.

read_img_flatten = imread('https://i0.wp.com/pythonguides.com/content/WhiteHouse.jpg',flatten = True)
read_img_flatten[0]

Now view the flattened image using the below code.

plt.imshow(read_img_flatten)
Python Scipy Ndimage Imread Flatten
Python Scipy Ndimage Imread Flatten

This is how to flatten the image using the parameter flatten with the method imread() of Python Scipy.

Read: How to use Python Scipy Gaussian_Kde

Python Scipy Ndimage Imread Mode

The method imread() of Python Scipy accepts a parameter mode that converts images to different modes like RGB. An image’s mode is a string that specifies the type and depth of each pixel. Each pixel takes advantage of the entire bit depth. Thus, the range of a 1-bit pixel is 0–1, that of an 8–bit pixel is 0–255, and so on.

The Python Imaging Library (PIL) is used by imread to read images. The mode may be one of the strings listed below:

  • ‘L’ (8-bit pixels, black and white)
  • ‘P’ (8-bit pixels, mapped to any other mode using a color palette)
  • ‘RGB’ (3×8-bit pixels, true color)
  • ‘RGBA’ (4×8-bit pixels, true color with transparency mask)
  • ‘CMYK’ (4×8-bit pixels, color separation)
  • ‘YCbCr’ (3×8-bit pixels, color video format)
  • ‘I’ (32-bit signed integer pixels)
  • ‘F’ (32-bit floating point pixels)

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

Import the required libraries or methods using the python code.

from scipy.ndimage import imread
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

Read the image of the USA White House as an array form and View the read image as an array using the method imshow() matplotlib using the below code.

read_img = imread('https://i0.wp.com/pythonguides.com/content/WhiteHouse.jpg')
plt.imshow(read_img

Now check the mode of the image using the method Image.open('path_of_image').mode of library PIL using the below code.

Image.open('https://i0.wp.com/pythonguides.com/content/WhiteHouse.jpg').mode

The above code shows the mode of the Image which RGB.

Python Scipy Ndimage Imread Mode Example
Python Scipy Ndimage Imread Mode Example

Change the mode of the image to P using the below code.

read_img_mode = imread('https://i0.wp.com/pythonguides.com/content/WhiteHouse.jpg',mode = 'P')
plt.imshow(read_img_mode)

The above code changes the color of the image according to the mode P.

Python Scipy Ndimage Imread Mode
Python Scipy Ndimage Imread Mode

This is how to change the mode of the image using the parameter mode of method imread() of Python Scipy.

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

We have covered how to read the image, and also learned about changing the mode or flattening the image using the Python Scipy method with the following topics.

  • Python Scipy Ndimage Imread
  • Python Scipy Ndimage Imread Deprecated
  • How to use the Scipy Ndimage Imread to Flatten the image
  • How to change the mode of the image