Clear Filters
Clear Filters

How to run all the image with format jpg in folder? and save into folder?

2 views (last 30 days)
Hello. Anyone know how to run a folder with thousand images instead of run one by one the image? and save into folder?
I=imread('11_285.jpg');
imshow(I)
magnificationFactor = 1.2;
J=imresize(I,magnificationFactor, 'bilinear');
imshowpair(I,J,method="montage")
imwrite(J,'11_285_rescale.jpg');

Accepted Answer

Walter Roberson
Walter Roberson on 22 Apr 2024
dinfo = dir('*.jpg');
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
magnificationFactor = 1.2;
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_rescale" + ext);
I = imread(thisfile);
J = imresize(I, magnificationFactor, 'bilinear');
imwrite(J, outfile);
end
  5 Comments
Dayangku Nur Faizah Pengiran Mohamad
ok thanks sir. I get it now. But now I want to crop my image. Here's my codes:
mypath = '/home/motmot/Downloads/All data/275/Ground truth/Rescale image/';
dinfo = dir(fullfile(mypath,'*_275.jpg'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_crop" + ext);
x_cent= 450;
y_cent= 260;
size_of_cropped_img=511;
centroide=[x_cent y_cent];
I3=imread(thisfile);
xmin=x_cent-size_of_cropped_img/2;
ymin=y_cent-size_of_cropped_img/2;
I4=imcrop(I3,[xmin ymin size_of_cropped_img size_of_cropped_img]);
imshow(I4)
imwrite(I4, outfile);
end
The image for outfile for cropping image did not save in the folder. How can I save the crop image?
Dayangku Nur Faizah Pengiran Mohamad
OK sir. I get the idea. I forgot to put 'rescale' at back the code
from this,
dinfo = dir(fullfile(mypath,'*_275.jpg'));
into this,
dinfo = dir(fullfile(mypath,'*_275_rescale.jpg'));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!