How to save image from one folder to another?
13 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(3 个)
Image Analyst
2014-5-14
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>
0 个评论
Rahul punk
2021-6-2
编辑:Rik
2021-6-2
%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') ;
0 个评论
Image Analyst
2021-6-2
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!