Comment afficher une image avec Matplotlib Python
![Comment afficher une image avec Matplotlib Python](/img/Matplotlib/feature-image---Display-an-image-with-matplotlib-python.webp)
Ce tutoriel explique comment vous pouvez utiliser la méthode matplotlib.pyplot.imshow()
pour afficher des images avec Matplotlib.
La méthode matplotlib.pyplot.imshow()
est
matplotlib.pyplot.imshow()
affiche les chiffres dans Matplotlib.
matplotlib.pyplot.imshow()
Syntaxe
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)
Ici, X
représente une structure de type tableau de l’image affichée ou l’image PIL
.
Exemples : Afficher une image avec Matplotlib Python en utilisant imshow()
import matplotlib.pyplot as plt
import matplotlib.image as img
image = img.imread("lena.jpg")
plt.imshow(image)
plt.show()
Production:
Il lit l’image lena.jpg
dans le répertoire de travail courant en utilisant la méthode imread()
du module matplotlib.image
et affiche finalement l’image en utilisant la méthode imshow()
. Vous devez appeler la méthode show()
après imshow()
pour afficher l’image si vous n’utilisez pas IPython Notebooks
; la méthode show()
lancera une fenêtre séparée de l’image.
Exemples : Afficher une image PIL
avec Matplotlib Python en utilisant imshow()
import matplotlib.pyplot as plt
from PIL import Image
image = Image.open("lena.jpg")
plt.imshow(image)
plt.show()
Production:
Il affiche l’image PIL
. Nous la lisons en utilisant la méthode open()
du module Image
de PIL
. Nous pouvons également afficher directement l’image en utilisant PIL
d’une manière beaucoup plus simple.
from PIL import Image
img = Image.open("lena.jpg")
img.show()
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn