I would like to add noise to every image inside a folder and save them to a new directory afterwards, I have a function that i found in another question that adds the exact noise that i need, however i need to load and save the images with noise added to them, in different directories:
AttributeError: 'list' object has no attribute 'shape'
The function actually uses arrays, and i am using PIL to load images, how can i get around this issue?
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'
I tried using PIL but PIL deals with the image as an object, whereas this function is trying to deal with the images by treating them as arrays.