简体   繁体   English

从子文件夹中读取所有图像,调整它们的大小并在 Python 中使用它们的原始名称保存它们?

[英]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.

相关问题 如何从子文件夹中读取所有图像,调整它们的大小并将它们以原始名称保存在 Python 中? - How to read all images from subfolder, resize them and save them with their original name in Python? 从子文件夹中读取所有图像,检测文本并将它们与原始名称和文件夹一起保存在 Python 中? - Read all images from subfolder, detect text them and save them with their original name and folder in Python? Python/PIL 调整所有子文件夹中所有图像的大小并保存为新文件名 - Python/PIL Resize all images in all subfolder and save as new file name 如何调整图像大小并使用 python 保存它们? - how can I resize images and save them with python? 如何从一个文件夹中调整多个图像的大小,将它们转换为灰度并将它们保存到另一个文件夹中? - How to resize multiple images from a folder, convert them into grayscale and save them into another folder? 使用 glob (Python) 读取多个图像并将它们保存在 csv 中 - Read multiple images using glob (Python) and save them in csv 从特定目录读取多个图像,使用 python 和 opencv 将它们保存在另一个目录中 - Read multiple images from specific directory, prepossessed and save them another directory using python and opencv 如何下载网页的所有图片并以原始名称保存? - How to download ALL the pictures of a webpage and save them in their original names? 将所有图像调整大小后如何在Python(PIL)中保存大量图像? - How can I save lots of images after resizing them all into another directory in Python(PIL)? Python:如何读取多个文件夹中的所有文本文件内容并将其保存到一个excel文件中 - Python: How to read all text file contents in multiple folders and save them into one excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM