Skip to content
Related Articles

Related Articles

Improve Article

Getting Started With ImageIO Library in Python

  • Last Updated : 20 Aug, 2020

Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, volumetric data, and scientific formats. It is cross-platform.

Installation:

This module does not come built-in with Python. To install it type the below command in the terminal.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

pip install imageio

Requirements:

  • Python 3.5+
  • Numpy 
  • Pillow 

For accessing input images files click here. Let’s see the code for some working of imageio library:



1) Read an image: For reading an image we have to used imageio.imread() method.

Syntax: imageio.imread(“filename or path”)

Parameter:

  •  filename/path: Absolute or Relative path of the image file.

Return: returns a numpy array, which comes with a dict of meta data at its ‘meta’ attribute.

Example:

Python3




# import library
import imageio
  
# read an image
image = imageio.imread('testa.png')
  
# print shape of the image
print(image.shape) 

Output:

(134, 151, 4)

2) Reading GIF file: For reading a GIF file we have to used imageio.get_reader() method.



Syntax: imageio.get_reader(filename, format, mode)

Parameters:

  • path/filename: FileName of required image/file/gif .
  • format: str, The format to used to read the file, By default imageio select the appropriate for you ,based on the filename and its contents.
  •  mode: {‘i’, ‘I’, ‘v’, ‘V’, ‘?’}
    • Used to give the reader a hint on what the user expects (default “?”):
    • ‘i’ for an image
    • I’ for multiple images
    • ‘v’ for a volume
    • ‘V’ for multiple volumes
    • ?’ for don’t care    

Returns: Reader`object which can be used to read data and meta-data from the specified file.

Example:

Python3




# import library
import imageio
  
# create an image object
image = imageio.get_reader('test.gif')
  
# iterating through matrix:
for i in image :
    
  # Each frame is a numpy matrix
  print(i.shape)

Output:

(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)
(300, 354, 4)

3) Creating Image file: For creating an image file we have to used imageio.imwrite() method.

Syntax: imageio.imwrite(filename,numPy_ndarray, format=None) 

Parameters:

  • filename: Path / Name of file to be saved as
  • numpy_ndarray: The image data. Must be NxM, NxMx3 or NxMx4.
  • format: str, The format to used to read the file. By default, imageio selects, the appropriate for you, based on the filename and its contents.

Example:

Python3




# import required libraries
import imageio
import numpy as np
  
  
rows, cols = (5, 5
  
# create numpy 2-d array
arr = np.zeros((rows, cols))
  
# create an image
image = imageio.imwrite('image_writing.png'
                     arr)

Output:

image_writing.png.png

As Data-values were zeros so this is empty image with 68 bytes size.




My Personal Notes arrow_drop_up
Recommended Articles
Page :