Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.2 kB
2
Indexable
Never
import os

# Define the paths to your Image and Mask folders
image_folder = 'Image'
mask_folder = 'Mask'

# List all files in the Image and Mask folders
image_files = os.listdir(image_folder)
mask_files = os.listdir(mask_folder)

# Iterate through the files and rename them
for image_file, mask_file in zip(image_files, mask_files):
    # Extract the common part from the filenames
    common_part = ''
    common_parts = image_file.split('_')[3:]
    for part in common_parts:
        common_part += part
        common_part += '_'
    common_part = common_part.replace('.tif_', '')
    
    # Rename the files
    new_image_name = f'{common_part}.tif'
    new_mask_name = f'{common_part}.tif'

    # Construct the full paths for renaming
    old_image_path = os.path.join(image_folder, image_file)
    new_image_path = os.path.join(image_folder, new_image_name)
    old_mask_path = os.path.join(mask_folder, mask_file)
    new_mask_path = os.path.join(mask_folder, new_mask_name)

    # Rename the files
    os.rename(old_image_path, new_image_path)
    os.rename(old_mask_path, new_mask_path)

print("Files renamed successfully.")