read and rename a group of photos
2 次查看(过去 30 天)
显示 更早的评论
Hello every body,
I have two sets of groups of photos with around 1000 pictures for each set. Anyhow, both of them have the same sequence of names, they start from Image00000_IR1 to Image00999_IR1. What I want is rename the second set of pictures, so they should start from Image01000_IR1 to Image01999_IR1, so I could put them together in one folder and they will start from Image00000_IR1 to Image01999_IR1.
Is there a way that we could that in matlab?
Thanks in advance.
0 个评论
采纳的回答
Image Analyst
2021-1-2
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Specify the output folder.
outputFolder = fullfile(myFolder, 'Renamed');
if ~isfolder(outputFolder)
% Create the output folder if it doesn't exist.
mkdir(outputFolder);
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
inputBaseFileName = theFiles(k).name;
inputFullFileName = fullfile(theFiles(k).folder, inputBaseFileName);
fprintf(1, 'Now reading file %d of %d : "%s"\n', k, length(theFiles), inputFullFileName);
% Extract the number between indexes 6 and 11.
if length(inputBaseFileName) < 11
% Name is not correct because it's too short.
fprintf(1, ' Skipping file "%s" because it does not have a number.\n', inputBaseFileName);
continue;
end
inputNumber = str2double(inputBaseFileName(6:11));
if isnan(inputNumber)
% Does not have a 5 digit number so skip it.
fprintf(1, ' Skipping file "%s" because it does not have a number.\n', inputBaseFileName);
continue;
end
% For the output number, add a thousand to it
outputNumber = sprintf('%5.5d', inputNumber + 1000); % a string.
% Create the output name
outputBaseFileName = [inputBaseFileName(1:5), outputNumber, inputBaseFileName(12:end)];
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Now read it in as an image array with imread()
imageArray = imread(inputFullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Make a copy with the new filename in the output folder.
fprintf(1, ' Copying it to %s\n', outputFullFileName);
copyfile(inputFullFileName, outputFullFileName);
end
fprintf('Done running %s.m.\n', mfilename);
10 个评论
Image Analyst
2021-1-2
11 is the underline, so just include from 11 onwards:
outputBaseFileName = [inputBaseFileName(1:5), outputNumber, inputBaseFileName(11:end)];
更多回答(1 个)
Image Analyst
2021-1-1
See the FAQ:
In the middle of the loop, put a call to movefile() to do renaming.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!