34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
import os
|
||
|
||
# ================== 配置参数 ==================
|
||
# 图片所在的文件夹路径
|
||
image_folder = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/ailaidete/bag' # 修改为你的图片文件夹路径
|
||
|
||
# 输出的txt文件路径
|
||
output_txt = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/ailaidete/bag/image_list.txt' # 修改为你想保存的路径
|
||
|
||
# 支持的图片格式
|
||
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.webp', '.tiff', '.gif'}
|
||
|
||
# 是否保存绝对路径(True)还是仅文件名(False)
|
||
save_full_path = True # 设为 False 只保存文件名
|
||
# =============================================
|
||
|
||
# 获取所有图片文件
|
||
image_files = []
|
||
for root, dirs, files in os.walk(image_folder):
|
||
for file in files:
|
||
ext = os.path.splitext(file.lower())[1]
|
||
if ext in image_extensions:
|
||
file_path = os.path.join(root, file)
|
||
if save_full_path:
|
||
image_files.append(os.path.abspath(file_path))
|
||
else:
|
||
image_files.append(file)
|
||
|
||
# 写入txt文件
|
||
with open(output_txt, 'w', encoding='utf-8') as f:
|
||
for img_path in image_files:
|
||
f.write(img_path + '\n')
|
||
|
||
print(f"✅ 已保存 {len(image_files)} 张图片的路径到:\n {output_txt}") |