Getting Started With ImageIO Library in Python - GeeksforGeeks

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.
This module does not come built-in with Python. To install it type the below command in the terminal.
pip install imageio
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 libraryimport imageio  # read an imageimage = imageio.imread('testa.png')  # print shape of the imageprint(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 imageI’ for multiple images‘v’ for a volume‘V’ for multiple volumes?’ for don’t care    
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 libraryimport imageio  # create an image objectimage = 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 librariesimport imageioimport numpy as np    rows, cols = (5, 5)   # create numpy 2-d arrayarr = np.zeros((rows, cols))  # create an imageimage = 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.
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()