-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyRandomImages.py
More file actions
43 lines (31 loc) · 1.23 KB
/
CopyRandomImages.py
File metadata and controls
43 lines (31 loc) · 1.23 KB
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
32
33
34
35
36
37
38
39
40
41
42
import os
import shutil
import random
import re
import sys
def find_and_copy_jpgs(input_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
file_groups = {}
for root, dirs, files in os.walk(input_dir):
for file in files:
if file.lower().endswith('.jpg'):
base_name = re.sub(r'_\d{3}\.jpg$', '', file)
if base_name not in file_groups:
file_groups[base_name] = []
file_groups[base_name].append(os.path.join(root, file))
for group, files in file_groups.items():
random_file = random.choice(files)
relative_path = os.path.relpath(os.path.dirname(random_file), input_dir)
target_folder = os.path.join(output_dir, relative_path)
if not os.path.exists(target_folder):
os.makedirs(target_folder)
shutil.copy(random_file, target_folder)
print(f'Copying file: {random_file} -> {target_folder}')
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py <input_folder> <output_folder>")
sys.exit(1)
input_folder = sys.argv[1]
output_folder = sys.argv[2]
find_and_copy_jpgs(input_folder, output_folder)