-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_images.py
34 lines (24 loc) · 1.22 KB
/
rename_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import shutil
import csv
source_folder = "/path/to/source/folder" # Specify the path to the folder containing the subfolders
destination_folder = "/path/to/destination/folder" # Specify the path to the destination folder
csv_file = "/path/to/output.csv" # Specify the path to the CSV file
with open(csv_file, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Folder", "Image Names"])
for folder_num in range(100): #for example you have 100 folders
folder_path = os.path.join(source_folder, str(folder_num))
if not os.path.isdir(folder_path):
continue # Skip if the folder doesn't exist
image_files = os.listdir(folder_path)
new_image_names = []
for i, image_file in enumerate(image_files):
old_path = os.path.join(folder_path, image_file)
new_filename = "{}_{}.jpg".format(folder_num, i+1)
new_image_names.append(new_filename)
new_path = os.path.join(destination_folder, new_filename)
shutil.copy2(old_path, new_path)
new_image_names_str = ",".join(new_image_names)
writer.writerow([folder_num, new_image_names_str])
print("Image renaming and moving completed!")