En plus d'utiliser opencv en python, vous pouvez également utiliser matplotlib et PIL pour exploiter des images. Je préfère matpoltlib car sa syntaxe ressemble plus à matlab.
1. matplotlib
1. Afficher des images
1 2 3 4 5 6 7 8 9 10 11 | import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
lena = mpimg.imread('lena.png') # 读取和代码处于同一目录下的 lena.png
# 此时 lena 就已经是一个 np. array 了,可以对它进行任意处理
lena.shape #(512, 512, 3)
plt.imshow(lena) # 显示图片
plt.axis('off') # 不显示坐标轴
plt.show()
|
Copier après la connexion
2. Afficher une chaîne
1 2 3 4 5 6 7 8 9 10 11 | # 显示图片的第一个通道
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
# 此时会发现显示的是热量图,不是我们预想的灰度图,可以添加 cmap 参数,有如下几种添加方法:
plt.imshow('lena_1', cmap='Greys_r')
plt.show()
img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot' 是热量图
plt.show()
|
Copier après la connexion
3. Convertir une image RVB en image en niveaux de gris
Il n'existe pas de fonction appropriée dans matplotlib pour convertir une image RVB en image en niveaux de gris. Vous pouvez en personnaliser une selon la formule :
1 2 3 4 5 6 7 8 | def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
gray = rgb2gray(lena)
# 也可以用 plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.imshow(gray, cmap='Greys_r')
plt.axis('off')
plt.show()
|
Copier après la connexion
4 . l'image
Scipy est utilisé ici
1 2 3 4 5 | from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) # 第二个参数如果是整数,则为百分比,如果是tuple,则为输出图像的尺寸
plt.imshow(lena_new_sz)
plt.axis('off')
plt.show()
|
Copier après la connexion
5 Enregistrer l'image
5.1 Enregistrer l'image dessinée par matplotlib
Cette méthode convient. pour enregistrer toute image dessinée par matplotlib, équivalente à une capture d'écran.
1 2 3 | plt.imshow(lena_new_sz)
plt.axis('off')
plt.savefig('lena_new_sz.png')
|
Copier après la connexion
5.2 Enregistrer le tableau en tant qu'image
1 2 | from scipy import misc
misc.imsave('lena_new_sz.png', lena_new_sz)
|
Copier après la connexion
5.3 Enregistrer le tableau directement
Après la lecture, vous pouvez toujours afficher l'image selon la méthode précédente d'affichage du Cette méthode n'entraînera aucune perte de qualité de l'image
1 2 | np.save('lena_new_sz', lena_new_sz) # 会在保存的名字后面自动加上.npy
img = np.load('lena_new_sz.npy') # 读取前面保存的数组
|
Copier après la connexion
2. PIL
1 Afficher l'image
1 2 3 | from PIL import Image
im = Image.open('lena.png')
im.show()
|
Copier après la connexion
2. Image PIL dans un tableau Numpy
1 2 | im_array = np. array (im)
# 也可以用 np.asarray(im) 区别是 np. array () 是深拷贝,np.asarray() 是浅拷贝
|
Copier après la connexion
3. Enregistrer l'image PIL
Appelez directement la méthode de sauvegarde de la classe Image
1 2 3 | from PIL import Image
I = Image.open('lena.png')
I.save('new_lena.png')
|
Copier après la connexion
4. image
Ici, matplotlib.image est utilisé pour lire le tableau d'images. Notez que le tableau lu ici est de type float32, avec une plage de 0-1, tandis que les données PIL.Image sont de type uinit8, avec une plage de 0 à 255, il doit donc être converti :
1 2 3 4 5 | import matplotlib.image as mpimg
from PIL import Image
lena = mpimg.imread('lena.png') # 这里读入的数据是 float32 型的,范围是0-1
im = Image.fromarray(np.uinit8(lena*255))
im.show()
|
Copier après la connexion
5. Convertir RVB en image en niveaux de gris
1 2 3 4 5 | from PIL import Image
I = Image.open('lena.png')
I.show()
L = I.convert('L')
L.show()
|
Copier après la connexion
Ce qui précède représente l'intégralité du contenu de cet article. J'espère que cela sera utile à l'apprentissage de tout le monde. J'espère également que tout le monde apprendra le chinois PHP.
Pour plus d'articles connexes sur deux méthodes de lecture et d'affichage d'images en python, veuillez faire attention au site Web PHP chinois !