2025-09-11 20:44:35 +08:00
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
# ================== 配置参数 ==================
|
|
|
|
|
|
# 图片所在的文件夹路径
|
2026-03-10 13:58:21 +08:00
|
|
|
|
image_folder = '/home/hx/yolo/yemian/61_lianghua' # 修改为你的图片文件夹路径
|
2025-09-11 20:44:35 +08:00
|
|
|
|
|
|
|
|
|
|
# 输出的txt文件路径
|
2026-03-10 13:58:21 +08:00
|
|
|
|
output_txt = '/home/hx/yolo/yemian/61_lianghua/image_list.txt' # 修改为你想保存的路径
|
2025-09-11 20:44:35 +08:00
|
|
|
|
|
|
|
|
|
|
# 支持的图片格式
|
|
|
|
|
|
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}")
|