Read image and save it using imwrite?

Hello! I read pictures using imread and then try to write it to folder c:\result. The name of the images should be same than readed image, but I like to add index and remove part of the name. So I read image image00123_big.bmp and I like to save image to name image00123_small_'index_number'.bmp. How can I do that?

 Akzeptierte Antwort

ChristianW
ChristianW am 14 Mär. 2013

2 Stimmen

Input_folder = '.\'; % folder with big images
Output_folder = 'c:\result';
D = dir([Input_folder 'image*_big.bmp']);
Inputs = {D.name}';
Outputs = Inputs; % preallocate
for k = 1:length(Inputs)
X = imread([Input_folder Inputs{k}]);
idx = k; % index number
Outputs{k} = regexprep(Outputs{k}, 'big', ['small_' num2str(idx)]);
imwrite(X, [Output_folder Outputs{k}],'bmp')
end

Weitere Antworten (1)

Alessandro
Alessandro am 14 Mär. 2013
Bearbeitet: Alessandro am 15 Mär. 2013

1 Stimme

id = 5;
folder = 'c:\result\'
newimagename = [folder 'image00123_small_' num2str(id) '.bmp'];
imwrite(image,newimagename)

4 Kommentare

Teemu
Teemu am 14 Mär. 2013
How it´s working if filename is not same all the time, it will be like image00123_big.bmp, image00124_big.bmp and so on? and imwrite should be image00123_small.bmp, image00124_small.bmp. So it takes filename from readed filename.
Alessandro
Alessandro am 14 Mär. 2013
Bearbeitet: Alessandro am 15 Mär. 2013
strid = regexp(readimagename,'\d','match')
id = str2num(strid(1));
folder = 'c:\result\'
newimagename = [folder 'image00123_small_' num2str(id) '.bmp'];
imwrite(image,newimagename)
Jan
Jan am 14 Mär. 2013
Do not shadow the important Matlab function path by a local variable. This can leed to very strange effects e.g. during debugging.
Image Analyst
Image Analyst am 14 Mär. 2013
Use "folder" instead of "path" for a variable name. Don't use "dir" either - it's a built in function.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by