how to convert images from a folder and save in other

5 views (last 30 days)
i try to convert images from a folder to my cumputer . I must make all the images with dimensions 227x227 and after that save the converted images an other folder. Can someone give me an advice or a piece of code?

Answers (1)

Mathieu NOE
Mathieu NOE on 25 Mar 2022
hello Claudia
try this and adapt it to your needs
% select appropriate options below and adapt code to your specific needs ...
% files = dir('*.tif'); % select all tiff files in current directory
files = dir('handheld*.tif'); % select only tiff files with "handheld" in the filename
% resize options (sacle factor or pixels)
scale_factor = 0.5;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 120;
y_dim = 80;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, scale_factor);
% select (crop)
H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.tiff'];
imwrite(H{ck},fullfile(out_dir,filename));
end

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!