如何将一批处理的图像保存到特定文件夹
我正在尝试处理RAW数据集Sufolder,并将输出图像保存到目标文件夹中。
因此:例如:
Video_0001中的RAW数据集应保存在目标目录中,并使用与Video_0001相同的文件夹名称保存,
我尝试了以下代码,但是由于数据集包含200多个文件夹,
因此我可以提出的内容
directory = "C:\\Users\\dataset\\distdir"
save_directory1 = "C:\\Users\\Desktop\\dataset\\distdir\\save_img\\Folder1"
save_directory2 = "C:\\Users\\Desktop\\dataset\\distdir\\save_img\\Folder2"
height = 512
width = 512
for root, dirs, files in os.walk(directory):
for folder_name in dirs:
cv2.imwrite(os.path.join(save_directory1, file), img)
cv2.imwrite(os.path.join(save_directory2, file), img)
for file in files:
img = cv2.imread(os.path.join(root,file))
print(img)
img = cv2.resize(img, (height, width))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX)
print(img.shape)
,但是输出是文件夹中的输出。 1和文件夹2仅具有最后一个图像,并且该解决方案不可行,因为我有200多个文件夹。
任何想法都将不胜感激
I am trying to process a raw dataset sufolder and save the output images to a destination folder.
So for example:
Raw dataset in video_0001 should be saved in destination directory with the same folder name as video_0001
I tried the following code but since the dataset contain over 200 folders
here's what I was able to come up with
directory = "C:\\Users\\dataset\\distdir"
save_directory1 = "C:\\Users\\Desktop\\dataset\\distdir\\save_img\\Folder1"
save_directory2 = "C:\\Users\\Desktop\\dataset\\distdir\\save_img\\Folder2"
height = 512
width = 512
for root, dirs, files in os.walk(directory):
for folder_name in dirs:
cv2.imwrite(os.path.join(save_directory1, file), img)
cv2.imwrite(os.path.join(save_directory2, file), img)
for file in files:
img = cv2.imread(os.path.join(root,file))
print(img)
img = cv2.resize(img, (height, width))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX)
print(img.shape)
But the output is that in folder 1 and folder 2 has the last image only and this solution is not feasible as I have over 200 folders.
Any thoughts would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将两个循环组合起来。否则,您将覆盖变量IMG并一次又一次地文件,并且只能保存最后处理的IMG。
尝试:
You need to combine the two for loops. Otherwise, you'll overwrite the variables img and file again and again and will only save the last processed img.
Try:
您可以首先定义预处理逻辑。
中,例如,这是您来源的树目录 使用此解决方案的文件夹
您可以处理200个子文件夹
You can define your preprocess logic first.
For example, this is the tree directory of your origin folder
With this solution you able to handle 200 subfolders