-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_20.py
More file actions
59 lines (53 loc) · 2.38 KB
/
Copy pathscratch_20.py
File metadata and controls
59 lines (53 loc) · 2.38 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import shutil
import csv
# 假设CSV文件路径和图片文件夹路径已知
csv_file_path = r'C:\Users\ADMIN\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\cards.csv'
images_folder_path = r'C:\Users\ADMIN\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\output_images'
# 用于存储CSV数据的字典
data_dict = {}
# 读取CSV文件并存储数据
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# 将数据存储为字典,以data-id为键
data_dict[row['data-id']] = {
'data-provision': int(row['data-provision']),
'data-color': row['data-color'],
'data-type': row['data-type']
}
# 创建分类文件夹
folders = ['folder_1', 'folder_2', 'folder_3']
for folder in folders:
if not os.path.exists(folder):
os.makedirs(folder)
# 遍历图片文件夹中的所有文件
for filename in os.listdir(images_folder_path):
if filename.startswith('card_image_'):
# 从文件名中提取data-id
data_artid = filename.split('card_image_')[1].split(".")[0]
# 检查data-artid是否存在于CSV数据中
if data_artid in data_dict:
data = data_dict[data_artid]
# 根据特殊情况先判断是否应该放入一号文件夹
if data['data-type'] in ['ability', 'leader']:
print(data_artid)
target_folder = 'folder_1'
# 然后按照data-color的规则分类
elif data['data-color'] == 'gold':
target_folder = 'folder_1'
else:
target_folder = 'folder_2' if data['data-provision'] != 0 else 'folder_3'
# 构建图片的完整源路径和目标路径
src_path = os.path.join(images_folder_path, filename)
dst_path = os.path.join(target_folder, filename)
file2name = "card_description_"+filename.split('card_image_')[1]
src2_path = os.path.join(images_folder_path, file2name)
dst2_path = os.path.join(target_folder, file2name)
# 移动图片到相应的文件夹
shutil.copy(src_path, dst_path)
shutil.copy(src2_path, dst2_path)
print(f'Moved {filename} to {target_folder}')
else:
print(f'Data-id {data_id} not found in CSV file.')
print('Classification complete.')