read and rename a group of photos

6 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 2 Jan 2021
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 Comments
Image Analyst
Image Analyst on 2 Jan 2021
11 is the underline, so just include from 11 onwards:
outputBaseFileName = [inputBaseFileName(1:5), outputNumber, inputBaseFileName(11:end)];
Mohanned Al Gharawi
Mohanned Al Gharawi on 3 Jan 2021
It worked, I really appreciate your help. Thank you.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 1 Jan 2021
See the FAQ:
In the middle of the loop, put a call to movefile() to do renaming.
  1 Comment
Mohanned Al Gharawi
Mohanned Al Gharawi on 1 Jan 2021
Image Analyst thank you for replying, I didn't get it very well. Is there any other way to do that?
Thank you

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!