Reputation:
I have around 40 folders with 50 images inside each folder that i am adding noise to them. What i am doing is rewritting the path for every time i need to add noises to the 50 images and also write the path where the folder will be saved.
For instance:
loadimage_with_noise('C:/Users/Images/folder 1/')
This will load every image inside folder 1, but i have folders up to 40, so i have to keep rewritting all the paths all the way to folder 40th
the code that does it:
def loadimage_with_noise(path):
filepath_list = listdir(path)
for filepath in filepath_list:
img = Image.open(path + filepath)
img = img.resize((81, 150))
img = np.asarray(img)
noise_image = generate_noisy_image(img, 0.10)
noise_image = Image.fromarray(noise_image)
noise_image = noise_image.convert("L")
folder_index = 'folder 2/'
noise_image.save('C:/Users/Images/Noise/'+folder_index +filepath, 'JPEG')
function that adds the noise
def generate_noisy_image(x, variance):
noise = np.random.normal(0, variance, (150, 81))
return x + noise
What i want to do is automate this task, instead of passing as parameter a new path for every folder inside my folder of images, i'd like to simply: for every folder, load every image inside and save them in a new folder (the noise is already being added), for every folder inside my Images folder
So for each 40 folders with 50 images, i will have new 40 folders with 50 images in a different directory
Upvotes: 2
Views: 82
Reputation: 375
You can use os.walk
import os
for root, dirs, files in os.walk("C:/Users/Images/"):
for name in files:
image_path = os.path.join(root, name)
process_your_image(image_path)
Upvotes: 1