Clear Filters
Clear Filters

How to make all images in a folder the same pre-determined size

14 views (last 30 days)
Hey I need to make 120 images the same size (800 pix (width) by 600 pix (height)). The pictures differ in ratio, so instead of stretching/skewing them, I'd like to cut equal amounts from the outer edge.
Is there a way of doing this in matlab (i.e. first compressing images to width of 800, then chopping height to 600)?
Thanks,
Isabel
  2 Comments
Walter Roberson
Walter Roberson on 31 Jan 2014
If the images differ in aspect ratios then one of them might have a height less than 600 after the width has been adjusted to 800.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 31 Jan 2014
Inside the loop, figure out the size reduction factor and call imresize, then get the new size and crop if necessary. Something like this (untested):
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Get initial size
[rows, columns, numberOfColorChannels] = size(imageArray);
% Get size reduction / magnification factor
sizeFactor = 800 / columns;
% Resize
newImage = imresize(imageArray, sizeFactor);
% Get the new size.
[rows, columns, numberOfColorChannels] = size(imageArray);
% Crop if necessary
if rows > 600
% Take upper 600 lines. You could take lower or middle 600 also.
newImage = imcrop(imageArray, [1,1,columns, 600]);
newFileName = strrep(fullFileName, '.jpg', '_resized.jpg');
imwrite(newImage, newFileName );
end
end

Shivaputra Narke
Shivaputra Narke on 31 Jan 2014
Edited: Walter Roberson on 31 Jan 2014
Hope this helps.....
contents=ls;
for i=1:length(contents)
try
tempImage=imread(contents(i,:));
tempImage=imresize(tempImage,[600,800]);
extnposn=find(contents(i,:)=='.');
extn=contents(i,extnposn+1:end);
extn(extn==' ')=[];
name=contents(i,:);
name(name==' ')=[];
imwrite(tempImage,name,extn)
catch
%write err msg as
%use msgbox
end
end

Walter Roberson
Walter Roberson on 31 Jan 2014
[rows, cols, panes] = size(tempImage);
aspratio = rows ./ cols;
now if aspratio < 3/4 then you need to resize so height becomes 600 and then chop width down to 800; otherwise you need to resize so width becomes 800 and then chop height down to 600.

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!