change size of images
6 views (last 30 days)
Show older comments
I have a database of thousands of images of different sizes like 45x78, 67x89, 83x99 etc. how to make all the images of same size?
0 Comments
Accepted Answer
KSSV
on 26 Mar 2021
Read about imresize. Run a loop for each image and change them and save them if you want using imwrite.
More Answers (1)
Image Analyst
on 27 Mar 2021
@Jyoti Nautiyal, try this full demo. If it works, could you Vote for the Answer:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% FAQ reference:
% https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
% Specify the folder where the files live.
inputFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(inputFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', inputFolder);
uiwait(warndlg(errorMessage));
inputFolder = uigetdir(); % Ask for a new one.
if inputFolder == 0
% User clicked Cancel
return;
end
end
outputFolder = fullfile(inputFolder, 'Resized');
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Specify how many rows and columns you want the output image to be.
desiredRows = 60;
desiredColumns = 90;
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(inputFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
subplot(2, 1, 1);
imshow(imageArray); % Display image.
axis('on', 'image');
caption = sprintf('Original "%s"', baseFileName);
title(caption, 'Interpreter', 'none');
% Resize the image
resizedImageArray = imresize(imageArray, [desiredRows, desiredColumns]);
subplot(2, 1, 2);
imshow(resizedImageArray); % Display image.
axis('on', 'image');
caption = sprintf('Resized "%s"', baseFileName);
title(caption, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it to the output folder
outputFullFileName = fullfile(outputFolder, baseFileName);
imwrite(resizedImageArray, outputFullFileName);
end
fprintf('Done running %s.m\n', mfilename);
0 Comments
See Also
Categories
Find more on Convert Image Type 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!