How to save output image

Hi there,
I do have a batch of image files. I do know how to save them in a folder using the directory option. However, I need to know if I can save the output image with the same title as input image using a loop as it will be a batch process and therefore; I do not want to process one image and save it manually each time.
As for example if my input image is (lets say : image-10-00, image-10-01, image_10_02 >>> I can save them as process_1, process_2,process_3 serially but not exactly as the same input image title).
Any advice will be appreciated.

 Accepted Answer

Siam - If you want to use the same image file name (or a variation of it), then try the following - use fileparts to get the path, name of the file, and extension, and work from there
filename = '/Users/geoff/somePath/image-00-001.jpg';
[folderName,name,ext] = fileparts(filename);
In this example, we see that the three fileparts output parameters are set to
folderName =
/Users/geoff/somePath
name =
image-00-001
ext =
.jpg
You can now build the new file name as
newFileName = sprintf('%s_1%s',name,ext);
fullFileName = fullfile(folderName, newFileName);
which will set fullFileName to be
fullFileName =
/Users/geoff/somePath/image-00-001_1.jpg
Or, you can replace the folderName with a new destination folder, and keep the original file name.

4 Comments

Siam
Siam on 14 Oct 2014
I am using this
  • M = dir(fullfile(imgPath,'*.jpg'));
  • mkdir(destination_folder);
  • a=length(M);
  • for d = 1:length(M)
  • file_name = sprintf('Processed_image_%d.jpg',d);
Not sure how to use fileparts or name to save as same as input image title.
Any advice will be appreciated.
I think that you need to clarify what you mean by input image title. Is this the name of the file, or something else? Because, if it is the file name, then the code becomes
myJpgs = dir(fullfile(imgPath,'*.jpg'));
mkdir(destination_folder);
for d = 1:length(myJpgs)
srcFileName = fullfile(imgPath, myJpgs(d).name);
[~,filename,ext] = fileparts(srcFileName);
newFileName = sprintf('%s_1%s',filename,ext);
destFileName = fullfile(destination_folder,newFileName);
copyfile(srcFileName,destFileName);
end
Siam
Siam on 14 Oct 2014
Edited: Siam on 14 Oct 2014
Yes, it is the name of the file.
You have already given me the answer.
This is what I am looking for.
Thank you very much.
Glad it worked out, Siam!

Sign in to comment.

More Answers (0)

Asked:

on 14 Oct 2014

Edited:

on 21 Oct 2014

Community Treasure Hunt

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

Start Hunting!