Rename jpg files in a folder

19 Ansichten (letzte 30 Tage)
Ravin Rayeok
Ravin Rayeok am 23 Okt. 2020
Verschoben: Voss am 7 Mär. 2023
I need some help here,
So I have like 200 Images with the name of 1 2 3 4 ... n
Ineed the file to be renamed as 001 002 .... 012... 034 .... 180 ... 200
How to do that?
Thanks,

Akzeptierte Antwort

Image Analyst
Image Analyst am 23 Okt. 2020
Try this:
folder = pwd; % Wherever your input images are stored.
outputFolder = fullfile(folder, '/renamed'); % Can be the same as the input folder if you want.
% Make output folder if it exists.
if ~isfolder(outputFolder)
mkdir(outputFolder)
end
fileList = dir('*.bmp')
% Loop over all files.
for k = 1 : length(fileList)
% Get this filename.
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
% Find out just the base filename without the folder.
[~, baseFileNameNoExt, ext] = fileparts(thisFileName);
number = str2double(baseFileNameNoExt);
% Skip non-numbers
if isnan(number)
continue;
end
fprintf('Processing %s...\n', thisFileName);
% Optionally display the image
% % Read in the input image.
% originalImage = imread(thisFileName);
% % Display it.
% imshow(originalImage);
% drawnow;
% Create the new base filename without the folder.
baseOutputFileName = sprintf('%3.3d%s', number, ext);
% Create output image filename....
fprintf(' Renaming to %s to %s...\n', baseFileNameNoExt, baseOutputFileName);
% Create the output filename which will be in the output folder.
outputFileName = fullfile(outputFolder, baseOutputFileName);
movefile(thisFileName, outputFileName); % or imwrite()
end
message = sprintf('Done processing %d images', length(fileList))
uiwait(helpdlg(message));
If you want to save the renamed files in a different folder, use imwrite(), otherwise if you want to rename in the same folder use movefile().
  4 Kommentare
Niladri
Niladri am 7 Mär. 2023
Verschoben: Voss am 7 Mär. 2023
How to automatically change the name of several IMAGE files (in .bmp) stored in a folder?
Folder name is: gauss and filenames are stored as: GAUSS0, GAUSS1, GAUSS2,......etc.
I want to make them: GAUSS501, GAUSS502, GAUSS503,.... etc. in the same/different folder.
A quick help will be appreciated!
Thank you.
Niladri
Image Analyst
Image Analyst am 7 Mär. 2023
Verschoben: Voss am 7 Mär. 2023
@Niladri see code below and make appropriate changes.
% Optional initialization steps
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 long g;
format compact;
folder = pwd; % or 'C:\Users\niladri\pictures'
if ~isfolder(folder)
msgboxw('Warning: folder does not exist.\n"%s"\n\nPlease pick a new folder.', folder);
folder = uigetdir;
if ~isfolder(folder)
return;
end
end
filePattern = fullfile(folder, 'GAU*.bmp');
fileList = dir(filePattern)
if isempty(fileList)
msgboxw('Warning: No .BMP files found in:\n"%s"\n\nPlease pick a new folder.', folder);
folder = uigetdir;
if ~isfolder(folder)
return;
end
end
allFileNames = {fileList.name}
for k = 1 : numel(allFileNames)
baseFileName = allFileNames{k};
thisFileName = fullfile(folder, baseFileName);
fprintf('Checking "%s".\n', thisFileName)
% Change fro GAUSS1 to GAUSS501 by replacing the first 5 characters
newBaseFileName = strrep(baseFileName, 'GAUSS', 'GAUSS50');
outputFullFileName = fullfile(folder, newBaseFileName);
% Rename the .xlsx
fprintf(' Renaming "%s" to "%s"\n', baseFileName, newBaseFileName);
movefile(thisFileName, outputFullFileName);
end
if ispc
winopen(folder);
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by