matplotlib

Image manipulation

Opening images

Matplotlib includes the image module for image manipulation

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

Images are read from file (.png only) with the imread function:

img = mpimg.imread('my_image.png')

and they are rendered by the imshow function:

plt.imshow(img)

Let’s plot the Stack Overflow logo:

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img = mpimg.imread('so-logo.png')
plt.imshow(img)
plt.show()

The resulting plot is Simple plot of the SO logo


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow