How to save images (or variables) with original names in a new directory in MATLAB?
I wrote some code below but I don't know how to proceed from this point:
Images = dir('C:\Users\Mehran\Desktop\Affin dic\Pubfig\dev croped\*.jpg');
for i=1:length (Images)
ImgName=strcat('C:\Users\Mehran\Desktop\Affin dic\Pubfig\dev croped\',Images(i).name);
Img=((imread(ImgName)));
Img=imresize(Img,[100 100]);
????
end
Just like reading in images with imread
, use imwrite
to write image files to disk. Images
has all of the files listed in that directory. As such, I suggest you create a new directory and store all of these images resized using Images
that you created earlier.
Images = dir('C:\Users\Mehran\Desktop\Affin dic\Pubfig\dev croped\*.jpg');
outDirectory = 'C:\Users\Mehran\Desktop\Affin dic\Pubfig\dev croped\resized\'; %// New - for output directory
%// New - Make directory
mkdir(outDirectory);
for i=1:length (Images)
ImgName=strcat('C:\Users\Mehran\Desktop\Affin dic\Pubfig\dev croped\',Images(i).name);
Img=((imread(ImgName)));
Img=imresize(Img,[100 100]);
imwrite(Img, strcat(outDirectory, Images(i).name)); %// Change here
end
The above code is modified so that a new output directory is created. Specifically, in your dev croped
directory, I've made a subdirectory called resized
that will store the resized images, then we cycle through each image, resize the image, then write the image to the output directory using the same name as each original image.
If you want to make your code platform independent, it may be a good idea that you use fullfile
to help you create the output directory you want as a chain of other directories pieced together:
%// New, specify base directory
baseDirectory = fullfile('C:', 'Users', 'Mehran', 'Desktop', 'Affin dic', 'Pubfig', 'dev croped');
%// Change
Images = dir(fullfile(baseDirectory, '*.jpg'));
%// New - for output directory
outDirectory = fullfile(baseDirectory, 'resized');
%// New - Make directory
mkdir(outDirectory);
for i=1:length (Images)
ImgName=fullfile(baseDirectory, Images(i).name); %// Change
Img=((imread(ImgName)));
Img=imresize(Img,[100 100]);
imwrite(Img, fullfile(outDirectory, Images(i).name)); %// Change here
end
This to me is more readable... but again, that's my opinion.
JPEG compression is lossy and so loading in the image which is already in JPEG, resizing it and then resaving it will give you more quality loss than it is worth. If you're insistent on using JPEG to write the images, set the 'Quality'
flag to 100 when using the imwrite
function. This will minimize some of the error and trade off for less compression performed at the expense of a larger file size, but given how cheap storage is today, I think you won't mind:
%// New, specify base directory
baseDirectory = fullfile('C:', 'Users', 'Mehran', 'Desktop', 'Affin dic', 'Pubfig', 'dev croped');
%// Change
Images = dir(fullfile(baseDirectory, '*.jpg'));
%// New - for output directory
outDirectory = fullfile(baseDirectory, 'resized');
%// New - Make directory
mkdir(outDirectory);
for i=1:length (Images)
ImgName=fullfile(baseDirectory, Images(i).name); %// Change
Img=((imread(ImgName)));
Img=imresize(Img,[100 100]);
imwrite(Img, fullfile(outDirectory, Images(i).name), 'Quality', 100); %// Change here
end