How to change file name and convert the file

3 ビュー (過去 30 日間)
Nathan Payne
Nathan Payne 2018 年 11 月 26 日
コメント済み: Nathan Payne 2018 年 11 月 27 日
Hello,
I have written (borrowed from another user) a very simple code to open a file, resize it, convert to grayscale then save it back into my with a new format and name.
I1=imread('D0159.jpg')
I2 = imresize(I1,[256 256]);
In=rgb2gray(I2)
figure; imshow(In)
imwrite(In,'CottonFiber0016.png')
What I would like to do is automate this. I have seen other people do similar, however I can not make it work. I have a lot of files in a folder and I need them to open, convert and save as new name and autoincrement the number at the end of the file name. You can see I stopped at 16 because I realized there must be a faster way.
Thanks for your help

採用された回答

Stephen23
Stephen23 2018 年 11 月 26 日
編集済み: Stephen23 2018 年 11 月 26 日
S = dir('*.jpg');
for k = 1:numel(S)
im = imread(S(k).name);
im = imresize(im,[256,256]);
im = rgb2gray(im);
fn = sprintf('CottonFiber%04d.png',k);
imwrite(im,fn)
end
See also:
  1 件のコメント
Nathan Payne
Nathan Payne 2018 年 11 月 27 日
This is perfect. Thank you so much.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by