Image handling using the Python Matplotlib library

MasadAshraf
3 min readFeb 7, 2023

--

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.

Image Processing Example

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:
Plot image output
Generated Plot image

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

Reference

Matplotlib Documentation

If you like my article, consider buying me a coffee here or visit my homepage. Thanks

--

--

MasadAshraf
MasadAshraf

Written by MasadAshraf

Hola 👋! AI Expert & building apps with remarkable technologies. Taste and try new things for fun growth mixed with Coding!

No responses yet