English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Image

The image module in the Matplotlib package provides the functions required to load, resize, and display images. The Pillow library supports loading image data. Matplotlib only supports PNG images. If the local reading fails, the command displayed below will revert to Pillow.

The image used in this example is a PNG file, but remember the Pillow data requirements. The imread() function is used to read float32 image data in the ndarray object of dtype.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
import matplotlib.pyplot as plt
 import matplotlib.image as mpimg
 import numpy as np
 img = mpimg.imread('mtplogo.png')

Assume that there is an image named mtplogo.png in the current working directory.

Any array containing image data can be saved to a disk file by executing the imsave() function. Here, the original png file's vertical flip version is saved by setting the origin parameter to lower.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
plt.imsave("logo.png", img, cmap='gray', origin='lower')

If the image is opened in the image viewer, the new image will be displayed as follows.

To draw an image on the Matplotlib viewer, please execute the imshow() function.