Files
zjsh_yolov11/yemian/lianghua_txt.py
琉璃月光 df7c0730f5 bushu
2025-10-21 14:11:52 +08:00

34 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
# ================== 配置参数 ==================
# 图片所在的文件夹路径
image_folder = '/home/hx/yolo/yemian/resize_p' # 修改为你的图片文件夹路径
# 输出的txt文件路径
output_txt = '/home/hx/yolo/yemian/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}")