1
votes

I have a folder containing 106 images. Of these, 15 have a '.jpg' extension and the other 91 have a '.tif' extension. I want to rename the files sequentially so that they all have the '.tif' extension. I am able to rename the files with same extension (.jpg) using the following code:

 a = 'D\newfolder';
 A = dir(fullfile(a,'*.jpg'));
 fileNames = {A.name};
 for iFile = 1:numel(A)
    newName = fullfile(a,sprintf('%0d.jpg',iFile));
    movefile(fullfile(a,fileNames{iFile}),newName);
end

This code works fine for images with the '.jpg' extension. However, now I have both '.tif' and '.jpg' file extensions in the same folder. I want to rename them and apply further operations to them within a loop. How can I modify this code to rename both '.tif' and '.jpg' files sequentially and with '.tif' as the renamed files' extension?

1
Do you want to keep the extension as a .tif? Is the order supposed to go from 1 to 106 for all file names regardless of extension or is it okay to have 1.jpg to 15.jpg and then 1.tif to 91.tif?informaton
I have to keep the extension of all 106 files to tiff. I want to change jpg files to tiff. After that order should to be from 1 to 106.sidraaleem
Is it '.tif' like you say in your post or 'tiff' like you say in your comment? Let me know, and I'll put it that way in my answer.informaton
it is 'tif'. Sorry for typo.sidraaleem
Did my answer help?informaton

1 Answers

1
votes

Here's a solution that stays close to your current one:

A = dir(fullfile(a,'*.jpg'));
B = dir(fullfile(a,'*.tif'));

fileNames = {A.name,B.name};
for iFile = 1:numel(fileNames)
    newName = fullfile(a,sprintf('%0d.tif',iFile));
    movefile(fullfile(a,fileNames{iFile}),newName);    
end

Where a is the folder you have defined already.

UPDATE

The file extension '.jpg' and '.tif' are just labels indicating the image format expected within that file. There are just labels though and can be wrong. For example if you gave these files a '.txt' extension they do not suddenly become text files, though you are more likely to be able to open them up in a text editor and verify.

That being said, many image loaders will first verify the image format directly from the file (and not the extension) and use the corresponding codec to load it. So the mismatch in format and file extension may not actually matter in practice, but you are relying a bit on chance.

You can transcode your jpeg formatted images to tiff format directly however. A similar question was asked at The Mathworks site.

Essentially you would read the file and then write it in the new format. Here's how you can change the above code to do it:

A = dir(fullfile(a,'*.jpg'));
B = dir(fullfile(a,'*.tif'));

fileNames = {A.name,B.name};
for iFile = 1:numel(fileNames)
    [~,~, fileExt] = fileparts(fileNames{iFile});
    if(strcmpi(fileExt,'.jpg'))
        fullJpegFilename = fullfile(a,fileNames{iFile});
        tmpImg = imread(fullJpegFilename); % read jpeg format
        imwrite(tmpImg,fullfile(a,sprintf('%0d.tif',iFile)); % write file as .tif format
        delete(fullJpegFilename);  % no longer needed            
    else
        newName = fullfile(a,sprintf('%0d.tif',iFile));
        movefile(fullfile(a,fileNames{iFile}),newName);    
    end
end