我从互联网站加载了10,000个图像文件并保存在文件夹中以供使用(图像检索系统),现在我需要将图像文件重命名为顺序名称,例如(image1,image2,image3,.... image10000),有人可以帮助我吗?我想告诉您我在我的工作中使用了Matlab。谢谢。
需要记住的一件事是文件名中数字部分的格式,因为这有时会影响目录中文件的排序。例如,使用上述命名约定有时会导致类似于以下排序顺序:
image1.jpg
image10.jpg
image11.jpg
image2.jpg
image3.jpg
...
这通常不是您想要的。如果您将数字填充到最大数字大小(在您的情况下为5位数)并用零填充,目录中的排序顺序应该会更好地保持:
image00001.jpg
image00002.jpg
image00003.jpg
....
要创建这样的文件名,您可以使用SPRINTF函数。以下是一些示例代码,以此方式重命名目录中的所有.jpg文件:
dirData = dir('*.jpg'); %# Get the selected file data
fileNames = {dirData.name}; %# Create a cell array of file names
for iFile = 1:numel(fileNames) %# Loop over the file names
newName = sprintf('image%05d.jpg',iFile); %# Make the new name
movefile(fileNames{iFile},newName); %# Rename the file
end
在Matlab中重命名文件:
movefile('myfile.m','myfile2.m')
如果你需要获取文件夹中的所有文件,请尝试以下代码:
filelist = dir('*.jpg');
我少了一行
datafiles = dir('*.jpeg');
for i = 1:length(datafiles)
fileOut = strrep(datafiles(i).name, '0000.jpeg', '.jpeg');
movefile(datafiles(i).name, fileOut);
end