Cómo mostrar una imagen con Matplotlib Python

Este tutorial explica cómo puede utilizar el método matplotlib.pyplot.imshow()
para mostrar imágenes utilizando Matplotlib.
matplotlib.pyplot.imshow()
Método
matplotlib.pyplot.imshow()
muestra las figuras en Matplotlib.
matplotlib.pyplot.imshow()
Sintaxis
matplotlib.pyplot.imshow(X,
cmap=None,
norm=None,
aspect=None,
interpolation=None,
alpha=None,
vmin=None,
vmax=None,
origin=None,
extent=None, *,
filternorm=True,
filterrad=4.0,
resample=None,
url=None,
data=None,
**kwargs)
Aquí, X
representa una estructura en forma de array de la imagen mostrada o la imagen PIL
.
Ejemplos: Mostrar una imagen con Matplotlib Python usando imshow()
import matplotlib.pyplot as plt
import matplotlib.image as img
image = img.imread("lena.jpg")
plt.imshow(image)
plt.show()
Resultado:
Lee la imagen lena.jpg
del directorio de trabajo actual usando el método imread()
del módulo matplotlib.image
y finalmente muestra la imagen usando el método imshow()
. Debe llamar al método show()
después de imshow()
para ver la imagen si no está usando IPython Notebooks
; el método show()
lanzará una ventana separada de la imagen.
Ejemplos: Mostrar una imagen PIL
con Matplotlib Python usando imshow()
import matplotlib.pyplot as plt
from PIL import Image
image = Image.open("lena.jpg")
plt.imshow(image)
plt.show()
Resultado:
Muestra la imagen PIL
. Lo leemos usando el método open()
del módulo Image
de la PIL
. También podemos mostrar directamente la imagen usando PIL
de una manera mucho más simple.
from PIL import Image
img = Image.open("lena.jpg")
img.show()
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn