如何将多个子文件夹中的图片复制到一个普通文件夹中并重命名?

How to copy images from multiple sub-folders to a common folder and rename them?

假设以下源路径和目标路径:

dir_src = r"/path/to/folder_with_subfolders"
dir_dst = r"/path/to/destination_folder" 

目录“folder_with_subfolders”包含多个子文件夹及其相关文件,如下面的树状图所示:

├── folder_with_subfolders
│   ├── Subfolder_1
|   |   ├── Subfolder1_1
|   |   |   ├──Subfolder_with_patientID1
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
|   |   |   |   ├── 3.dcm
                    .......
│   ├── Subfolder_2
|   |   ├── Subfolder2_1
|   |   |   ├──Subfolder_with_patientID2
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
                    .......
│   ├── Subfolder_3
|   |   ├── Subfolder3_1
|   |   |   ├──Subfolder_with_patientID3
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
|   |   |   |   ├── 3.dcm
|   |   |   |   ├── 4.dcm
                    .......

可以看出,DICOM图像文件同名(1.dcm、2.dcm、3.dcm、...)但属于不同的患者。子文件夹“Subfolder_with_patientID1”、“Subfolder_with_patientID2”、“Subfolder_with_patientID3”具有特定于该患者的不同文件夹名称。我想将所有这些 DICOM 图像复制到目标目录,同时重命名它们,以便每个图像名称都附加该患者特定的子文件夹名称,即像 Subfolder_with_patientID1_1.dcm 的名称一样, Subfolder_with_patientID1_2.dcm 的名字等等。这是起始脚本:

import glob
import os

# Location with subdirectories
dir_src = r"/path/to/folder_with_subfolders/"
#destination loction to move all the files
dir_dst = r"/path/to/destination/"    
# Get List of all images
files = glob.glob(dir_src + '/**/**/**/*.dcm', recursive=True)     
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    filename=filename[:-4].replace(".",.........) + ".dcm"
    # Copy the file with os.rename
    if not os.path.exists(os.path.join(dir_dst, filename)):
        os.rename(file, os.path.join(dir_dst, filename))
  

您将要拆分子文件夹:

subfolder = os.path.dirname(file).split('/')[-1]

然后将子文件夹添加到您的重命名中:

os.rename(file, dir_dst + subfolder + '_' + filename)

因此该部分应如下所示:

import glob
import os

# Location with subdirectories
dir_src = r"/path/to/folder_with_subfolders/"
#destination loction to move all the files
dir_dst = r"/path/to/destination/"    
# Get List of all images
files = glob.glob(dir_src + '/**/**/**/*.dcm', recursive=True)     
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    subfolder = os.path.dirname(file).split('/')[-1]
    # Copy the file with os.rename
    if not os.path.exists(os.path.join(dir_dst, filename)):
         os.rename(file, dir_dst + subfolder + '_' + filename)

使用shutil.copy2复制文件。

import glob
import os
import shutil

# Location with subdirectories
dir_src = "/home/nponcian/Documents/folder_with_subfolders/"

# Destination location to copy all the files
dir_dst = "/home/nponcian/Documents/folder_with_subfolders_dest/"

# Get List of all images
files = glob.glob(dir_src + '/**/*.dcm', recursive=True)

# Create the destination directory
if not os.path.exists(dir_dst):
    os.mkdir(dir_dst)

# For each image
for file_name_src in files:
    # Let's say file_name_src is currently "/home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/2.dcm"

    file_dir = os.path.basename(os.path.dirname(file_name_src))  # Would be "Subfolder_with_patientID1"
    file_name = os.path.basename(file_name_src)  # Would be "2.dcm"

    file_name_dst = os.path.join(dir_dst, f"{file_dir}_{file_name}")  # Would be "/home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_2.dcm"

    shutil.copy2(file_name_src, file_name_dst)
    print(f"Copied:\n\tFr: {file_name_src}\n\tTo: {file_name_dst}")

脚本之前的文件树是 运行

$ tree
.
├── folder_with_subfolders
│   ├── Subfolder_1
│   │   └── Subfolder1_1
│   │       └── Subfolder_with_patientID1
│   │           ├── 1.dcm
│   │           ├── 2.dcm
│   │           └── 3.dcm
│   ├── Subfolder_2
│   │   └── Subfolder2_1
│   │       └── Subfolder_with_patientID2
│   │           ├── 1.dcm
│   │           └── 2.dcm
│   └── Subfolder_3
│       └── Subfolder3_1
│           └── Subfolder_with_patientID3
│               ├── 1.dcm
│               ├── 2.dcm
│               ├── 3.dcm
│               └── 4.dcm
└── script.py

10 directories, 10 files

脚本后的文件树 运行

$ tree
.
├── folder_with_subfolders
│   ├── Subfolder_1
│   │   └── Subfolder1_1
│   │       └── Subfolder_with_patientID1
│   │           ├── 1.dcm
│   │           ├── 2.dcm
│   │           └── 3.dcm
│   ├── Subfolder_2
│   │   └── Subfolder2_1
│   │       └── Subfolder_with_patientID2
│   │           ├── 1.dcm
│   │           └── 2.dcm
│   └── Subfolder_3
│       └── Subfolder3_1
│           └── Subfolder_with_patientID3
│               ├── 1.dcm
│               ├── 2.dcm
│               ├── 3.dcm
│               └── 4.dcm
├── folder_with_subfolders_dest
│   ├── Subfolder_with_patientID1_1.dcm
│   ├── Subfolder_with_patientID1_2.dcm
│   ├── Subfolder_with_patientID1_3.dcm
│   ├── Subfolder_with_patientID2_1.dcm
│   ├── Subfolder_with_patientID2_2.dcm
│   ├── Subfolder_with_patientID3_1.dcm
│   ├── Subfolder_with_patientID3_2.dcm
│   ├── Subfolder_with_patientID3_3.dcm
│   └── Subfolder_with_patientID3_4.dcm
└── script.py

11 directories, 19 files

日志

$ python3 script.py 
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_2/Subfolder2_1/Subfolder_with_patientID2/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID2_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_2/Subfolder2_1/Subfolder_with_patientID2/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID2_1.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/3.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_3.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_1.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/3.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_3.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/4.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_4.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_1.dcm