感谢:https://blog.csdn.net/saltriver/article/details/78882669
import cv2
import numpy as np
from matplotlib import pyplot as plt
peppers = cv2.imread('4.png',0)
row, column = peppers.shape
noise_salt = np.random.randint(0,256,(row,column))
noise_pepper = np.random.randint(0,256,(row,column))
rand = 0.05
noise_salt = np.where(noise_salt<rand*256,255,0)
noise_pepper = np.where(noise_pepper<rand*256,-255,0)
peppers.astype("float")
noise_salt.astype("float")
noise_pepper.astype("float")
salt = peppers
salt = peppers+noise_salt
salt = salt+noise_pepper
img = np.where(salt>255,255,salt)
img = np.where(salt<0,0,salt)
plt.subplot(121),plt.imshow(peppers),plt.title('origin')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img),plt.title('noise')
plt.xticks([]), plt.yticks([])
plt.show()
效果: