def noise(image, n=10000):
result = image.copy()
length, high = result.shape[:2]
for i in range(n):
# 根据随机值来指定x, y位置
x = np.random.randint(1, length)
y = np.random.randint(1, high)
# 改变x, y位置的值
if np.random.randint(2) == 0: # 取值为0或1
result[x, y] = 255
else:
result[x, y] = 0
return result
image = cv2.imread('图片路径')
cv2.imshow('original', image)
#设置等待
cv2.waitKey(0)
result = noise(image)
cv2.imshow('noise', result)
#设置等待
cv2.waitKey(0)
#关闭所有打开的窗口
cv2.destroyAllWindows()