Reading images is a fundamental step when performing operations like cropping, resizing, rotating, or applying filters using image processing tools. The process of reading images involves capturing the pixel values and metadata from an image file and representing them in a suitable data structure, such as a NumPy array or matrix.
In the Scikit-image library, the io module provides a function imread() for loading image files into memory as NumPy arrays. Once the image is loaded into memory as a NumPy array, we can get access to a wide range of image processing functions available in Scikit-image to perform tasks like filtering, segmentation, feature extraction, and much more.
The imread() method
The io.imread() method in Scikit-image is capable of reading images in various formats, including JPEG, PNG, TIFF, BMP, and more. The function internally utilizes different libraries based on the specific image format, such as imageio, PIL, or tifffile.
Syntax
Following is the syntax and parameters of this method −
skimage.io.imread(fname, as_gray=False, plugin=None, **plugin_args)
- Fname − A string representing the file name or path or a URL of the image.
- as_gray (optional) − If set to True, it converts color images to grayscale represented as 64-bit floats. Images that are already in grayscale format are not converted.
- plugin (optional) − A string specifying the plugin to use for reading the image. If the plugin parameter is not provided, the function will automatically try different plugins, starting with imageio, until a suitable one is found. However, if the file name (fname) has a “.tiff” extension, the tifffile plugin will be used by default.
- **plugin_args (optional) − Additional keyword arguments that are passed to the specified plugin.
Return Value
The method returns a NumPy array that represents the image. The array contains the pixel values, and its shape corresponds to the dimensions of the image. The color bands or channels of the image are stored in the third dimension of the array.
For instance, a grayscale image would have dimensions MxN, an RGB image would have dimensions MxNx3, and an RGBA image would have dimensions MxNx4.
Example 1
The following example demonstrates how to load an image using the file name.
import skimage from skimage import io # Read an image image = io.imread(''Images/logo.png'') # Display image properties from the image array print(''The following are the properties of the loaded image:'') print("Image shape:", image.shape) print("Image data type:", image.dtype) print("Number of color channels:", image.shape[2])
Input Image
Output
The following are the properties of the loaded image: Image shape: (225, 225, 4) Image data type: uint8 Number of color channels: 4
Example 2
The following example demonstrates how to load an image using the image URL.
import skimage from skimage import io # Read an image image = io.imread(''https://www.tutorialspoint.com/images/logo.png'') # Display image properties from the image array print(''The following are the properties of the loaded image:'') print("Image shape:", image.shape) print("Image data type:", image.dtype) print("Number of color channels:", image.shape[2])
Input Image
Output
The following are the properties of the loaded image: Image shape: (140, 568, 3) Image data type: uint8 Number of color channels: 3
Example 3
The following example demonstrates how to convert color images to grayscale by using the as_gray parameter of the imread() method.
import skimage from skimage import io # Read an image image = io.imread(''https://www.tutorialspoint.com/images/logo.png'', as_gray=True) # Display image properties from the image array print(''The following are the properties of the loaded image:'') print("Image shape:", image.shape) print("Image data type:", image.dtype)
Output
The following are the properties of the loaded image: Image shape: (140, 568) Image data type: float64