how to rename images (.bmp) merging information from another file?
2 views (last 30 days)
Show older comments
Hello, I am struggling writing a code to rename some images (.bmp). I have 900 images to rename; each of one has a different name. Names can be divided into 4 categories: Stim_HF_1.bmp, Stim_HF_1_RUL.bmp, Stim_LF_1.bmp, Stim_LF_1_RUL.bmp with the number changing for every image.
I need to rename each image adding the word that corresponds to that number. For example HF_1 corresponds to the word ‘desert’, while LF_1 corresponds to the word ‘safari’. Following what I would like to obtain: ‘Stim_HF_1.bmp’ into ‘HF_1_desert.bmp’ ‘Stim_HF_1_ RUL.bmp’ into ‘HF_1_desert_ RUL.bmp’ ‘Stim_LF_1.bmp’ into ‘LF_1_safari.bmp’ ‘Stim_LF_1_ RUL.bmp’ into ‘LF_1_safari_ RUL.bmp’
I have a separate file with 3 columns and 901 (headings included) rows. The columns represent number, HF, LF. In each row there is number and words HF and LF associated (e.g. 1, desert, safari).
I wonder if there is a way to rename each image merging the original filename with the information included in the file. Do you have any idea about how to proceed? Many thanks in advance!
0 Comments
Answers (1)
Guillaume
on 9 Jan 2015
Edited: Guillaume
on 9 Jan 2015
How about:
cd('path\to\bmpfiles');
renametable = readtable('renamefile.txt');
rowfun(@renamefile, renametable);
And in separate m-file
function renamefile(index, HFname, LFname)
sources = strrep({'Stim_HF_*.bmp', 'Stim_HF_*_RUL.bmp', 'Stim_LF_*.bmp', 'Stim_LF_*_RUL.bmp'}, '*', sprintf('%d', index));
destinations = [strrep({'HF_*.bmp', 'HF_*_RUL.bmp'}, '*', sprintf('%d_%s', index, HFname)), strrep({'LF_*.bmp', 'LF_*_RUL.bmp'}, '*', sprintf('%d_%s', index, LFname))];
cellfun(@movefile, sources, destinations);
end
4 Comments
Susan Lane
on 28 May 2017
Very nice answer Guillaume, thank you for the tip! Do you also have a tip on how a .bmp file can be opened and edited? I started learning to code, I am a big Windows user, and I would like to know if there is a way to do this: http://www.coreldraw.com/en/pages/bmp-file/ in code ? Thanks!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!