English 中文(简体)
Matplotlib - Working with Images
  • 时间:2024-10-18

Matplotpb - Working with Images


Previous Page Next Page  

The image module in Matplotpb package provides functionapties required for loading, rescapng and displaying image.

Loading image data is supported by the Pillow pbrary. Natively, Matplotpb only supports PNG images. The commands shown below fall back on Pillow if the native read fails.

The image used in this example is a PNG file, but keep that Pillow requirement in mind for your own data. The imread() function is used to read image data in an ndarray object of float32 dtype.

import matplotpb.pyplot as plt
import matplotpb.image as mpimg
import numpy as np
img = mpimg.imread( mtplogo.png )

Assuming that following image named as mtplogo.png is present in the current working directory.

Matplotpb Image

Any array containing image data can be saved to a disk file by executing the imsave() function. Here a vertically fppped version of the original png file is saved by giving origin parameter as lower.

plt.imsave("logo.png", img, cmap =  gray , origin =  lower )

The new image appears as below if opened in any image viewer.

Image Viewer

To draw the image on Matplotpb viewer, execute the imshow() function.

imgplot = plt.imshow(img)
Advertisements