文件夹下所有文件重命名
import os,cv2
path='/home/lqs/Downloads/dataset/TTK100/TTK100_VOC/VOCdevkit/VOC2007/JPEGImages'
save='/home/lqs/Downloads/dataset/TTK100/TTK100_VOC/VOCdevkit/VOC2007/new_pics'
totle=len(os.listdir(path))
for s,i in enumerate(os.listdir(path)):
newname=os.path.splitext(i)
full_name=newname[0].zfill(6)
newpath=full_name+newname[1]
# print(newpath,pic.shape,os.path.join(save,newpath))
os.rename(os.path.join(path,i),os.path.join(save,newpath))
print('{}/{} original name:{} --->rename to:{}'.format(s,totle,os.path.join(path,i),os.path.join(save,newpath)))
根据图片路径保存到新路径下保存
import os,cv2
# path='/home/lqs/Downloads/dataset/VOCdevkit/VOC2007/JPEGImages'
path='/home/lqs/Downloads/dataset/TTK100/TTK100_VOC/VOCdevkit/VOC2007/JPEGImages'
save='/home/lqs/Downloads/dataset/TTK100/TTK100_VOC/VOCdevkit/VOC2007/newfile'
totle=len(os.listdir(path))
for s,i in enumerate(os.listdir(path)):
pic=cv2.imread(os.path.join(path,i))
newname=os.path.splitext(i)
full_name=newname[0].zfill(6)
newpath=full_name+newname[1]
# print(newpath,pic.shape,os.path.join(save,newpath))
cv2.imwrite(os.path.join(save,newpath),pic)
print('{}/{}--->save to:{}'.format(s,totle,os.path.join(save,newpath)))