How to save image from one folder to another?

38 views (last 30 days)
Hi,
How are you all? I want to read an image from a folder using matlab. Then I want to save the image to another folder of same directory? Is it possible? Please let me know.

Answers (3)

Image Analyst
Image Analyst on 14 May 2014
Construct the filename with fullfile(), then call imwrite(). See the FAQ:
<http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F>

Rahul punk
Rahul punk on 2 Jun 2021
Edited: Rik on 2 Jun 2021
%read image test.jpg
tt= imshow('C:\Users\admin\Desktop\test.jpg') ;
%save your image other location with any name save desktop or any folder also
saveas(tt,'C:\Users\admin\Desktop\testimagesave.jpg') ;

Image Analyst
Image Analyst on 2 Jun 2021
Try this:
% Demo by Image Analyst to read in a folder of JPG images and
% copy them to a different folder in PNG format.
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 short g;
format compact;
fontSize = 14;
% Specify the folder where the files live.
inputFolder = pwd; % '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
% Specify the output folder.
outputFolder = fullfile(inputFolder, 'PNG Files');
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(inputFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullInputFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading %s\n', fullInputFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullInputFileName);
imshow(imageArray); % Display image.
caption = sprintf('File #%d of %d : "%s"', k, length(theFiles), baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Create the output file name. Change extension to .PNG.
[~, baseFileNameNoExt, ext] = fileparts(baseFileName);
fullOutputFilename = fullfile(outputFolder, [baseFileNameNoExt, '.PNG']);
copyfile(fullInputFileName, fullOutputFilename);
end
message = sprintf('Done copying %d files.', length(theFiles));
uiwait(helpdlg(message));
fprintf('Done running %s.m\n', mfilename);

Community Treasure Hunt

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

Start Hunting!

Translated by