Rename image file name in matlab

I download 10,000 image files from an Internet site, and I save it in a folder to use in my project (image search system), now I need to rename the image file to a serial name (image1, image2, image3, .... image10000), anyone can help me ... I would like to inform you that I used Matlab in my work.

give thanks

+3
source share
4 answers

One thing you want to keep in mind is how the format of the license plate part of the file name will look, as this can sometimes affect the ordering of files in a directory. For example, using the naming convention you specified above sometimes results in a sort order like this:

image1.jpg
image10.jpg
image11.jpg
image2.jpg
image3.jpg
...

This is usually not what you would like. If you instead added a number with zeros to the maximum size (in your case 5 digits), the sort order should be better supported in the directory:

image00001.jpg
image00002.jpg
image00003.jpg
....

To create file names like this, you can use the SPRINTF function . Here is an example code that renames all .jpg files in a directory as follows:

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

The above code also uses DIR and MOVEFILE (as mentioned in other answers).

+13

matlab:

movefile('myfile.m','myfile2.m')

, - :

filelist = dir('*.jpg');
+2

You can use the "movefile" function in matlab (the first parameter is the source name, the second parameter is the destination), or when you first write the image, you can specify the e file in your imwrite command.

In any case, I suspect that you will need to scroll through the list of directories, this can be done using the "dir" or "ls" functions.

+1
source

I have one line shorter

datafiles = dir('*.jpeg');
for i = 1:length(datafiles)
    fileOut = strrep(datafiles(i).name, '0000.jpeg', '.jpeg');
    movefile(datafiles(i).name, fileOut);
end
+1
source

Source: https://habr.com/ru/post/1727323/


All Articles