import os
from PIL import Image
def getFileList(file_path):
file_list = []
for file in os.listdir(file_path):
if file.endswith(".png") or file.endswith(".jpg"):
write_name = file.split('/')[-1]
file_list.append(os.path.join(file_path,write_name))
return sorted(file_list)
def getHWList( images_path):
file_list = []
for path in images_path:
image = Image.open(path)
file_list.append(list(image.size))
return file_list
def createFilelist(images_path, text_save_path, mode_name):
# 'data/images' 'data/outdoor.odgt' val
print(os.path.exists(text_save_path))
file_name = open(text_save_path, "w")
images_list = []
for eachname in os.listdir(images_path):
path = os.path.join(images_path,eachname,mode_name)
image_list = getFileList(path)
images_list.append(image_list)
#获取图片的长度和宽度信息
hw_list = getHWList( images_list[0])
#将图片的信息按照一定的格式逐行写入txt文件中
segm_path, img_path = images_list
for i in range(len(hw_list)):
flag = '{{"fpath_img":"{0}", "fpath_segm":"{1}", "width":"{2}", "height":"{3}"}}'.\
format(img_path[i],segm_path[i],hw_list[i][0],hw_list[i][1])
file_name.write(flag + '\n')
file_name.close()
if __name__ == "__main__":
mode = 'test'
mode_name = r'val' if 'test' in mode else 'train'
# 图片存放目录
images_path = r'data/'
# 生成图片列表文件的保存目录
txt_path = r'./'
txt_name = r'outdoor_'+mode_name+r'.odgt'
text_save_path = txt_path + '/' + txt_name
# 生成txt文件
createFilelist(images_path, text_save_path, mode_name)
# 'data/' 'data/outdoor.odgt' val
print('生成成功!')
图片生成odgt文件代码
于 2023-11-14 11:09:37 首次发布