python寻找图片并将图片复制到特定位置

用python遍历目录,获得目录下所有子目录的以tif结尾的文件的路径

您可以使用Python的os和fnmatch模块来实现这个任务。下面是一个示例代码,它将遍历指定目录下的所有子目录,并找到以.tif结尾的文件,并输出它们的路径。

import os
import fnmatch

# 指定目录路径
root = '/path/to/directory'

# 遍历目录树
for dirpath, dirnames, filenames in os.walk(root):
    for filename in fnmatch.filter(filenames, '*.tif'):
        # 输出文件的完整路径
        print(os.path.join(dirpath, filename))

在这个示例中,os.walk()函数将遍历指定目录下的所有子目录。对于每个子目录,它将返回三个值:目录路径、子目录名称列表和文件名称列表。我们在内部的循环中使用fnmatch.filter()函数来找到以.tif结尾的文件,然后使用os.path.join()函数将目录路径和文件名连接起来,得到文件的完整路径。最后,我们使用print()函数输出这个路径。

复制图片

要在Python中将图像复制到特定位置,您可以使用shutil模块中的copy函数。这个函数可以将文件从一个位置复制到另一个位置。

以下是一个将图像文件从一个文件夹复制到另一个文件夹的示例代码:

import shutil
import os

# 源文件路径
src_folder = '/path/to/source/folder'
src_file = 'image.jpg'
src_path = os.path.join(src_folder, src_file)

# 目标文件路径
dst_folder = '/path/to/destination/folder'
dst_path = os.path.join(dst_folder, src_file)

# 复制文件
shutil.copy(src_path, dst_path)

在这个例子中,您需要将src_folder和dst_folder替换为实际的文件夹路径,src_file为要复制的图像文件的文件名。然后使用os.path.join函数来将文件夹路径和文件名组合成完整的文件路径。

最后,使用shutil.copy函数将文件从源路径复制到目标路径。

请注意,如果目标文件夹中已经存在同名文件,那么它将被覆盖。如果您希望在复制文件之前检查目标文件夹中是否已经存在同名文件,可以使用os.path.exists函数进行检查。

多线程

(由于复制图片数量较多启用了多线程,如果是windows电脑运行报错的话,需要将num_workers参数设置为0):

import os
import fnmatch
import shutil
from concurrent.futures import ProcessPoolExecutor


def find_end_tif(root: str):
    """
    intro:
        find all the files end with tif in `root`. return the list.
    
    args:
        :param str root: path.
    
    return:
        :param list files: return a list with the path of all the pics.
    """
    # store files
    files = []

    # 遍历目录树
    for dirpath, dirnames, filenames in os.walk(root):
        for filename in fnmatch.filter(filenames, '*.tif'):
            # 输出文件的完整路径
            file_path = os.path.join(dirpath, filename)
            # print(file_path)
            files.append(file_path)

    print(len(files))
    return files


def copy_multi_files(files_path: str, cp_path: str, num_workers: int=2):
    """
    intro:
        fill the args that need to be used in multi thread.
    
    args:
        :param str files_path: source path.
        :param str cp_path: target_path.
        :param int num_workers: 
    
    return:
        void.
    """
    # fill the args to start multi processes
    args = [(file_path, os.path.join(cp_path, file_path.split('/')[-1])) for file_path in files_path]

    mp_copy_multi_files(args, num_workers=num_workers)


def mp_copy_multi_files(args, num_workers=0):
    with ProcessPoolExecutor(max_workers=num_workers) as ex:
        res = ex.map(copy_one_file, *zip(*args))
    # return list(res)  


def copy_one_file(src_path: str, dst_path: str):
    # 复制文件
    shutil.copy(src_path, dst_path)


if __name__=="__main__":
    # 指定目录路径
    path = "/Volumes/ymz/_paper_with_code_and_data/3_microDL/data/S-BIAD25/mouse/_img_568/p03"
    cp_path = "/Volumes/ymz/_paper_with_code_and_data/3_microDL/data/S-BIAD25/mouse/_img_568/_all/mouse_kidney_registered"
    files = find_end_tif(path)
    copy_multi_files(files, cp_path,)
    pass
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值