Open a series of images and save them in another folder

1 view (last 30 days)
Hi,I want to open images from one folder and save the 8-bit images in another existed folder using original name. Here is my code:
close all; rand('seed',0);
folder = 'F:' files = [folder filesep '\*.tif']
files = dir(files); n = numel(files)
for k=1:n
fullFileName = fullfile(folder, files(k).name);
idx=randi(k);
im = files(idx).name;
afm = imread(fullfile(folder,im));
I8bit =im2uint8(afm);
[pathstr,name,ext] = fileparts(fullFileName) ; filename = strcat(pathstr,name,'.tif');
my_file = "F:\8bit\filename";
imwrite(I8bit,filename)
end
But the images were not able to be saved in that folder, can anyone help me? Another question is my code can only open jpg files if I change hat to '*\.jpg', with tif (as shown above) not able to open. Can anyone give me a hand? Thank you so much in advance.

Accepted Answer

Image Analyst
Image Analyst on 17 Feb 2018
Edited: Image Analyst on 17 Feb 2018
You need to have two folder variables, one for input, and one for output. And you need two base file names, one for input and one for output. Then use full file to create two filenames:
fullInputFileName = fullfile(inputFolder, baseInputFileName);
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
baseInputFileName would be files(k).name.
See full demo below:
% Copies all the files from one folder to another folder.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
% Define input and output folders.
% CHANGE THESE FOLDER NAMES!!!!!!
inputFolder = pwd;
outputFolder = uigetdir(pwd);
if strcmp(outputFolder, inputFolder)
errorMessage = sprintf('Error: the output folder must be different than the input folder');
uiwait(warndlg(errorMessage));
return;
end
% Check to see that both folders exist.
if ~isdir(inputFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', inputFolder);
uiwait(warndlg(errorMessage));
return;
end
if ~isdir(outputFolder)
errorMessage = sprintf('Error: The following output folder does not exist:\n%s', outputFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of files to copy.
filePattern = fullfile(inputFolder, '*.*'); % All files.
% filePattern = fullfile(inputFolder, '*.m'); % m-files.
fileNamesToTransfer = dir(filePattern);
numFiles = length(fileNamesToTransfer);
% Do the copying.
for k = 1 : numFiles
% Get the base file name.
baseFileName = fileNamesToTransfer(k).name;
% Create the full input and output filenames.
fullInputFileName = fullfile(inputFolder, baseFileName);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf(1, 'Now copying file #%d of %d: %s to %s\n', ...
k, numFiles, fullInputFileName, fullOutputFileName);
copyfile(fullInputFileName, fullOutputFileName);
end
msgbox('Done copy files!', 'modal');
  2 Comments
Tian Tian
Tian Tian on 17 Feb 2018
May I ask you a question? I strictly followed the rule, but somehow the images were not saved properly, the second image was saved in its original name, but the image was the same as first image.
folder = 'F:\CoTh' files = [folder filesep '\*.tif'] files = dir(files); n = numel(files)
for k=1:n
fullFileName = fullfile(folder, files(k).name);
idx=randi(k);
files(k).name
im = files(idx).name;
afm = imread(fullfile(folder,im));
I8bit =im2uint8(afm);
[pathstr,name,ext] = fileparts(fullFileName); filename = strcat(pathstr,name,'.tif');
outputFolder='F:';
fullOutputFileName = fullfile(outputFolder,filename);
May you please tell me my problem? Thanks in advance.
imwrite(I8bit,filename);
end

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!