如何使用Python将多个子目录中的所有图像复制到与其对应的另一个子目录中?我的数据集主文件夹包含N个子文件夹,每个子文件夹内有一组图像。我想对子目录中的每个图像应用处理,并将其移动到相应的子目录中,就像创建一个新的数据集图像表单一样。像下面这样的树:
+Home-dir
----- +sub-dir1 --->img1,img2,img3,------imgN
----- +sub-dir2 --->img1,img2,img3,------imgN
----- +sub-dir3 --->img1,img2,img3,------imgN
..
----- +sub-dirN --->img1,img2,img3,------imgN
我想得到以下信息:
+Home-dir-process
----- +sub-dir-new1 --->img-new1,img-new2,img-new3,------img-newN
----- +sub-dir-new2 --->img-new1,img-new2,img-new3,------img-newN
----- +sub-dir-new3 --->img-new1,img-new2,img-new3,------img-newN
..
----- +sub-dir-newN --->img-new1,img-new2,img-new3,------img-newN
我可以将一个目录中的所有图像复制到相应的一个目录中,如下所示:
path1='path\to\before\processing\image'
path2='path\to\save\after\processing\image'
listing = os.listdir(path1)
for file in listing:
im = Image.open(path1 + '\\' + file)
img = im.resize((img_rows, img_cols))
img.save(path2 + '\\' + file, "JPEG")
但是我想把多个子目录下的所有图片复制到另一个子目录下,有人能帮我吗?你知道吗
我修改了我原来的答案,因为它真的没有做你想要的,它基本上只做你的问题中的代码处理在一个文件夹中的所有图像文件。你知道吗
驱动程序函数名为
process_tree()
,它的任务是遍历主目录的子目录,查找其中与一组用户指定的文件名模式中的任何文件匹配的文件,如果找到任何文件,则创建目标子目录,然后对每个子目录调用用户提供的函数,向它传递现有的源文件名和所需的输出文件名,以及用户希望传递给所提供函数的任何参数。你知道吗在下面的代码中,用户指定的示例函数名为
resize_image()
。顾名思义,它只处理一个图像文件。你知道吗请注意,处理后的图像与源图像具有相同的名称(即没有
-_new
后缀添加到它们的文件名中)。你知道吗相关问题 更多 >
编程相关推荐