MATLAB Answers

Write an image name to particular folder using imwrite

462 views (last 30 days)
Dear users ..
i have been used an bottom in GUI to browse an image from computer .. but when i used imwrite, i can not write that image with its name.. so its not efficient to give name to that image each time .. is there any way to write image with its real name .. thanks
[fname path]=uigetfile('*.*');
if isequal(fname,0) | isequal(path,0)
warndlg('Please select an image from directory ..');
else
fname=strcat(path,fname);
Im2=imread(fname);
imwrite(Im2,'D:\imagefolder','jpeg');

  2 Comments

Suvetha Ravi
Suvetha Ravi on 25 Jan 2020
Can you give me a code that how can I store the resized and grayscaled images in a separate folder in a drive?Hoping for a reply!
Image Analyst
Image Analyst on 25 Jan 2020
Suvetha, yes - I already gave the code to ahmed. It is below - just scroll down or click here. Take note how there are both destinationFolder and folder (which is the input image folder).
Important Note: do not use path as the variable name for the folder like ahmed mistakenly did.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 30 Mar 2016
Edited: Image Analyst on 30 Mar 2016
Try this robust (but untested) code:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\Program Files\MATLAB' or wherever...
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullSourceFileName = fullfile(folder, baseFileName)
originalImage = imread(fullSourceFileName);
% Create destination filename
destinationFolder = 'D:\imagefolder';
if ~exist(destinationFolder, 'dir')
mkdir(destinationFolder);
end
% Strip off extenstion from input file
[sourceFolder, baseFileNameNoExtenstion, ext] = fileparts(fullSourceFileName);
% Create jpeg filename. Don't use jpeg format for image analysis!
outputBaseName = [baseFileNameNoExtenstion, '.JPG']
fullDestinationFileName = fullfile(destinationFolder, outputBaseName);
% Write the jpg file. This will convert whatever format you started with to the hated jpg format.
imwrite(originalImage, fullDestinationFileName);
Do not be afraid. It's well commented - just study it a line at a time.

  4 Comments

Show 1 older comment
Moucharaph SALAHOU
Moucharaph SALAHOU on 28 Dec 2018
please sir how about Writing many images name to particular folder using imwrite?
Moucharaph SALAHOU
Moucharaph SALAHOU on 28 Dec 2018
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file',...
'MultiSelect', 'on');
this is my code how can I save all the slected images into a folder...thank you for hepling
Walter Roberson
Walter Roberson on 28 Dec 2018
if ~iscell(baseFileName); baseFileName = {baseFileName}; end %user selected only one
filenames = fullfile(folder, baseFileName);
% Create destination filename
destinationFolder = 'D:\imagefolder';
if ~exist(destinationFolder, 'dir')
mkdir(destinationFolder);
end
% start saving
for K = 1 : length(filenames)
fullSourceFileName = filenames{K};
originalImage = imread(fullSourceFileName);
% Strip off extenstion from input file
[sourceFolder, baseFileNameNoExtenstion, ext] = fileparts(fullSourceFileName);
% Create jpeg filename. Don't use jpeg format for image analysis!
outputBaseName = [baseFileNameNoExtenstion, '.JPG']
fullDestinationFileName = fullfile(destinationFolder, outputBaseName);
% Write the jpg file. This will convert whatever format you started with to the hated jpg format.
imwrite(originalImage, fullDestinationFileName);
end

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Mar 2016
Use
fname=fullfile(path,fname);

  3 Comments

ahmed obaid
ahmed obaid on 30 Mar 2016
Thanks for your replay .. i got an error when using imwrite .. any help please .
global Im2;
global Im1;
axes(handles.axes1);
[fname path]=uigetfile('*.*');
if isequal(fname,0) | isequal(path,0)
warndlg('Please select an image from directory .. ','Ghasaq And Zahraa');
else
fname=strcat(path,fname);
Im1=fullfile(path,fname);
Im2=imread(fname);
imshow(Im2);
end
for write this ...
imwrite(Im1,'D:\imagefolder','JPG');

Sign in to comment.

Sign in to answer this question.

Tags

Products