打开图像并转化为矩阵,并显示
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img=np.array(Image.open('pokemon.jpg')) #打开图像并转化为数字矩阵
plt.figure("pokemon")
plt.imshow(img)
plt.axis('off')
plt.show()
打开图片,并随机添加一些椒盐噪声
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = np.array(Image.open('pokemon.jpg')) #打开图片
rows,cols,dims =img.shape
print()
for i in range(5000):
x = np.random.randint(0,rows)
y = np.random.randint(0,cols)
img[x,y,:] = 255
plt.figure('pokemon') #设置figure
plt.imshow(img)
plt.show()