博客原文链接:https://blog.csdn.net/weixin_44270815/article/details/105340549
```python
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 5 23:12:45 2020
@author: xbb
"""
import cv2
import numpy as np
import random
import glob
dir_path='C:/Users/hp/Desktop/1/' #读取图片路径
dir_path2='C:/Users/hp/Desktop/out/' #保存图片路径
img=cv2.imread(dir_path)
num=1
out_hight=512
out_wight=512
def salt(src,percentage):
NoiseImg=src
rows,cols,_=NoiseImg.shape
NoiseNum=int(percentage*rows*cols)
for i in range(NoiseNum):
randX=np.random.randint(0,rows-1)
randY=np.random.randint(0,cols-1)
if random.randint(0,1)<=0.5:
NoiseImg[randX,randY]=255
else:
NoiseImg[randX,randY]=NoiseImg[randX,randY]
return NoiseImg
for im in glob.glo