如何解决使用PIL和numpy加载和保存图像
我想向文件夹中的每个图像添加噪音,然后将它们保存到新目录,我在另一个问题中发现了一个函数,该函数添加了我需要的确切噪音,但是我需要加载并保存在不同目录中添加了噪点的图像:
AttributeError:“列表”对象没有属性“形状”
该函数实际上使用数组,并且我正在使用PIL加载图像,如何解决此问题?
def sp_noise(image,prob):
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
def loadimage(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
img = cv2.resize(img,(81,151))
images.append(img)
img = sp_noise(img,0.10)
cv2.imwrite(filepath,img)
img.save('C:/Users/Images/Image-set1/'+filepath,'JPEG')
return images
loadimage = 'root folder from the images to be loaded and added noise to'
我尝试使用PIL,但PIL将图像作为对象处理,而此功能试图通过将图像视为数组来处理图像。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。