In this article, we are going to depict images using matplotlib module in greyscale representation i.e. image representation using two colors only i.e. black and white.
- PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. PIL.Image.open() method in PIL module opens and identifies the given image file.
- Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. The matplotlib module can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc.
- Import required modules
- Displaying Original picture.
- Displaying Grayscale image, store the image path here let’s say it fname. Now open the image using PIL image method and convert it to L mode If you have an L mode image, that means it is a single-channel image – normally interpreted as greyscale. It only stores a greyscale, not color. Plotting the image as cmap = ‘gray’ convert the colours. All the work is done you can now see your image.
Below are some programs which depict how to display an image in grayscale using Matplotlib module:
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