如何将图像从多个目录复制到相应的多个目标目录并调整其大小? - python

如何使用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后缀添加到其文件名中)。

import fnmatch
import os
from PIL import Image

verbose = True  # Global printing flag for vprint().


def process_tree(src, dst, patterns, processing_func, *args):
    vprint(' src: "{src}"'.format(src=src))
    vprint('dst: "{dst}"'.format(dst=dst))
    vprint()
    for dirpath, subdirs, filenames in os.walk(src, topdown=False):
        vprint('PROCESSING dirpath: "{}"'.format(dirpath))
        if dirpath == src:  # Root src directory?
            if not os.path.exists(dst):
                vprint('CREATING dst root: "{dst}"'.format(dst=dst))
                os.makedirs(dst)  # Make root dest folder.
            vprint()
            continue  # Don't process files in root folder.

        # Determine sub-directory of src being processed.
        src_subdir = os.path.relpath(dirpath, src)
        dst_subdir = os.path.join(dst, src_subdir)

        # Determine which files in dirpath match one or more of the patterns.
        if isinstance(patterns, str):
            patterns = (patterns,)  # Convert to single element sequence.
        processible = set(filename for pattern in patterns
                                    for filename in fnmatch.filter(filenames, pattern))
        if not processible:
            vprint('no files to process')  # Output directory not created.
        else:
            if os.path.isdir(dst_subdir):
                vprint('PROCESSING directory "{}"'.format(dst_subdir))
            elif os.path.exists(dst_subdir):
                raise NotADirectoryError('Non-drectory "{}" exists"'.format(dst_subdir))
            else:
                vprint('CREATING directory "{}"'.format(dst_subdir))
                os.makedirs(dst_subdir)

            vprint('processing files:')
            for filename in filenames:
                if filename in processible:
                    src_file_path = os.path.join(dirpath, filename)
                    dst_file_path = os.path.join(dst_subdir, filename)
                    try:
                        processing_func(src_file_path, dst_file_path, *args)
                    except Exception as exc:
                        vprint('  EXCEPTION processing file:\n    {!s}'.format(exc))
                        vprint()
            vprint()


def resize_image(src, dst, scale_factor):
    """ Resize image src by scale_factor and save result to dst. """
    vprint('resizing image:\n'
           '  src: "{src}"\n'
           '  scale factor: {scale_factor}'.format(**locals()))
    img = Image.open(src)
    # Calcuate new size.
    new_width = round(img.width * scale_factor)
    new_height = round(img.height * scale_factor)
    if new_width < 1 or new_height < 1:
        vprint('  width and/or height of scaled version of image "{filename}"\n'
               '  is less than 1 pixel - skipped'.format(filename=os.path.basename(src)))
        return
    resampling_method = Image.BICUBIC
    img = img.resize((new_width, new_height), resample=resampling_method)
    img.save(dst)
    vprint('  resized image saved to "{}"'.format(dst))

def vprint(*args, **kwargs):
    """ Only prints if global flag is set. """
    if verbose:
        return print(*args, **kwargs)

if __name__ == '__main__':

    inputpath  = r'\my\path\to\_source_images'
    outputpath = r'\my\path\to\_processed_images'

    process_tree(inputpath, outputpath, ('*.jpg',), resize_image, .5)

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

如何修复AttributeError:模块'numpy'没有属性'square' - python

Improve this question 我已经将numpy更新为1.14.0。我使用Windows10。我尝试运行我的代码,但出现此错误: AttributeError:模块“ numpy”没有属性“ square”这是我的进口商品:%matplotlib inline import matplotlib.pyplot as plt import ten…