我有代码,通过一个文件夹'图像'和然后子文件夹和处理所有这些图像。在
我现在需要将这些图像保存到一个并行目录中,即一个名为“已处理图像”的文件夹(与“images”文件夹位于同一目录中),然后保存到该文件夹中的子文件夹中-这些子文件夹的名称与“images”中的子文件夹的名称相同-图像应保存到与其来源相同的子文件夹中。在
我可以将图像保存到“已处理图像”,但不能保存其中的子文件夹。在path = ("...\\Images")
for dirName, subdirList, fileList, in os.walk(path):
for file in fileList:
full_file_path = os.path.join(dirName, file)
if file.endswith((".jpg")):
image_file = Image.open(full_file_path)
image_file = image_file.convert('L')
image_file = PIL.ImageOps.invert(image_file)
image_file = image_file.resize((28, 28))
new_filename = file.split('.jpg')[0] + 'new.png'
path2 = ("...\\Processed Images")
image_file.save(os.path.join(path2, new_filename))
else: continue