Background:在做实验时,前期需要我们自己根据所得的图片制作数据集,这样就需要将图像从1开始进行编号,数量少的话人工标注很easy,但是一旦两位数三位数的样本数目时人工标注费时费力,下面的代码便可以解决这个问题。
import os
path = "D:\coriall"
filelist = os.listdir(path)
count=1
for file in filelist:
Olddir=os.path.join(path,file)
#print(Olddir)
if os.path.isdir(Olddir):
continue
filename=os.path.splitext(file)[0]
filetype=os.path.splitext(file)[1]
print(filename)
Newdir=os.path.join(path,str(count).zfill(3)+filetype)
os.rename(Olddir,Newdir)
count+=1