【发布时间】:2020-08-27 15:13:22
【问题描述】:
我有大约 40 个文件夹,每个文件夹中有 50 张图像,我正在向它们添加噪音。我正在做的是每次我需要为 50 张图像添加噪音时重写路径,并写下将保存文件夹的路径。
例如:
loadimage_with_noise('C:/Users/Images/folder 1/')
这将加载文件夹 1 中的每个图像,但我的文件夹最多为 40 个,所以我必须一直重写所有路径到第 40 个文件夹
执行此操作的代码:
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')
添加噪声的函数
def generate_noisy_image(x, variance):
noise = np.random.normal(0, variance, (150, 81))
return x + noise
我想要做的是自动执行此任务,而不是为我的图像文件夹中的每个文件夹传递一个新路径作为参数,我想简单地说:对于每个文件夹,加载每个图像并将它们保存在新文件夹(噪音已被添加),用于我的图像文件夹中的每个文件夹
因此,对于每 40 个包含 50 个图像的文件夹,我将在不同的目录中创建 40 个包含 50 个图像的新文件夹
【问题讨论】:
-
你已经知道函数
listdir。为什么不使用那个来迭代C:/Users/Images/中的所有文件夹? -
循环播放时我不知道如何保存它们
-
什么意思?我不明白你需要保存什么。因此,在文件夹
Images中,您有 x 个名称为folder {number}的文件夹,其中包含多个图像。你想把嘈杂的图像保存在哪里? -
我在读取图像时添加了噪声,现在用噪声保存图像,真的没有标准,只要它不是同一个文件夹,我只想:加载,添加噪音,保存
-
使用全局变量,在函数中使用额外的参数。找出你有什么问题。
标签: python numpy python-imaging-library