[英]Read all images from subfolder, resize them and save them with their original name in Python?
I want to read all images from a different subfolder, resize them and save them with their original name.我想从不同的子文件夹中读取所有图像,调整它们的大小并使用它们的原始名称保存它们。 However, I get the error shown below.
但是,我收到如下所示的错误。 How do I fix this error?
我该如何解决这个错误?
example code:示例代码:
from PIL import Image
import os
size = (112, 112)
def resize_img(path,dest_path):
name_list = []
label_list = []
resized_list = []
if not os.path.exists(dest_path):
os.makedirs(dest_path)
folder_list = os.listdir(path)
for num_imgs,folder in enumerate(folder_list):
print(str(num_imgs) + " : " + folder)
files_path = path + "/" + folder
image_list = os.listdir(files_path)
name_list.append(folder)
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
for image in label_list:
image = image.resize((112, 112))
resized_list.append(image)
for (i,new) in enumerate (resized_list):
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
resize_img('C:/Users/aunglay/PycharmProjects/FR_using_cnn/Face_database',
'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images')
Error appeared:出现错误:
Traceback (most recent call last):
File "C:/Users/aunglay/PycharmProjects/FR_using_cnn/smaple2.py", line 30, in <module>
'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images')
File "C:/Users/aunglay/PycharmProjects/FR_using_cnn/smaple2.py", line 27, in resize_img
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
File "C:\Users\aunglay\Anaconda3\envs\tensorflow_env\lib\site-packages\PIL\Image.py", line 2099, in save
fp = builtins.open(filename, "w+b")
OSError: [Errno 22] Invalid argument: 'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images/[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=64x64 at 0x2D7ACFD508>]_1.jpg'
The problem seems to be in the last line of the code:问题似乎出在代码的最后一行:
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
Specifically, label_list will be a list of image objects and will not contain the name of the image you just read.具体来说,label_list 将是一个图像对象列表,并且不包含您刚刚阅读的图像的名称。 Plus, I think the last two for loops are not necessary.
另外,我认为最后两个 for 循环不是必需的。 You could change the code:
您可以更改代码:
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
for image in label_list:
image = image.resize((112, 112))
resized_list.append(image)
for (i,new) in enumerate (resized_list):
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
to:到:
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
resized_list.append(img.resize((112, 112)))
resized_list[-1].save(dest_path+'/'+image_name+'.jpg')
And that should implement what you describe.这应该实现你所描述的。
edit: concerning what was asked in the comments:编辑:关于评论中提出的问题:
def resize_img(path,dest_path):
name_list = []
label_list = []
resized_list = []
folder_list = os.listdir(path)
for num_imgs,folder in enumerate(folder_list):
if not os.path.exists(dest_path+'/'+folder):
os.makedir(dest_path+'/'+folder)
print(str(num_imgs) + " : " + folder)
files_path = path + "/" + folder
image_list = os.listdir(files_path)
name_list.append(folder)
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
resized_list.append(img.resize((112, 112)))
resized_list[-1].save(dest_path+'/'+folder+'/'+image_name+'.jpg'
This should implement what you were describing.这应该实现您所描述的内容。 You need to call os.mkdir() for each of the sub-folders you want to create, thus it has to be inside the first for loop.
您需要为要创建的每个子文件夹调用 os.mkdir(),因此它必须位于第一个 for 循环内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.