Image handling using the Python Matplotlib library
Matplotlib is a 2D plotting library for the Python programming language. It provides functions for creating various types of plots, such as line plots, bar plots, scatter plots, histograms, pie charts, and many more. Matplotlib is widely used for data visualization and exploration in the field of data science and machine learning. The library is highly customizable, allowing users to fine-tune the appearance of their plots and create sophisticated visualizations.
Matplotlib can be used for image handling in Python through its imshow
function. imshow
allows you to display an image in a Matplotlib plot window. The function requires a 2D or 3D array-like object as input, representing the pixel values of the image.
For example, to display a grayscale image using Matplotlib, you can use the following code:
Results:
For color images, the input to imshow
should have shape (height, width, 3)
, with the third dimension representing red, green, and blue color channels. The color map can be specified using the cmap
argument.
In addition to image display, Matplotlib also provides functions for reading and saving images, for example, using the imread
and imsave
functions from the matplotlib.image
module.
Jpeg image loading or Handling
You can load RGB images using the python matplotlib like :
where ‘content/image.jpg’ is the file path.
Results:
(4032, 3024, 3)
You can now slice the R channel value from this using python slicing like :
Results:
Load the image as grayscale
Load images with operating system image viewer applications like :
Results:
Converting an image into Grey Scale
Results:
GitHub Link
https://github.com/MasadAshraf/Matplotlib