Write an image name to particular folder using imwrite

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 comentarios

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!
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 30 de Mzo. de 2016
Editada: Image Analyst el 30 de Mzo. de 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 comentarios

I edited it to make it even more robust - I added mkdir() to create the destination folder if it does not yet exist.
please sir how about Writing many images name to particular folder using imwrite?
[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
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

Iniciar sesión para comentar.

Más respuestas (2)

Use
fname=fullfile(path,fname);

3 comentarios

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');
What error?
Sajanaji
Sajanaji el 7 de Sept. de 2017
provide file name before '.jpg'

Iniciar sesión para comentar.

Akash kumar
Akash kumar el 16 de Mzo. de 2021
y = [1,1,1,1,1];
x = [2,2,2,2,2];
z = [2,2,2,2,2];
a = [3,3,3,3,3];
b = [4,4,4,4,4];
c = [5,5,5,5,5];
figure;
h(1)=plot(x,y);
figure;
h(2)=plot(x,z);
figure;
h(3)=plot(x,a);
figure;
h(4)=plot(x,b);
figure;
h(5)=plot(x,c);
for k=1:5
baseFileName = sprintf('figure_%d.png',k);
fullFileName = fullfile(cd,'image', baseFileName); % cd :-> for storing the image in current directory and image:-> is the folder % % name (First you creat the folder on that particular directory)
saveas(gcf,fullFileName);
end

Categorías

Más información sobre Manage Products en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 30 de Mzo. de 2016

Respondida:

el 16 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by