How to solve error A filename must be supplied in imwrite?

11 views (last 30 days)
I have multiple rgb images in a folder and i want to convert them into gray scale and the save them into another folder
code
ImageFolder = "D:\sys2\New folder\8PSK\8PSK"
imds = imageDatastore('D:\sys2\New folder\8PSK\*.png') % Whatever...
numberOfImages = length(imds.Files)
for k = 1 : numberOfImages
% Get the input filename. It already has the folder prepended so we don't need to worry about that.
inputFileName = imds.Files{k};
fprintf('Checking %s\n', inputFileName);
rgbImage = imread(inputFileName);
grayImage = rgb2gray(rgbImage);
imshow(grayImage);
file_name = sprintf('Image%d.png', k)% name Image with a sequence of number, ex Image1.png , Image2.png....
fullFileName = fullfile(ImageFolder, file_name);
imgName = [ImageFolder,'\Image_',num2str(k),'.png'] ;
imwrite(grayImage,imgName) ;
axis('on', 'image');
drawnow;
pause(0.1); % Short delay so we can see the image a little bit.
end
msgbox('Done!');
error
Error using imwrite>parse_inputs (line 610)
A filename must be supplied.
Error in imwrite (line 440)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Please help
  5 Comments
DGM
DGM on 14 Oct 2021
You're giving imwrite a vector of multiple strings. Unless behavior has changed since the version I'm using, that's not a valid input.

Sign in to comment.

Accepted Answer

yanqi liu
yanqi liu on 14 Oct 2021
Edited: yanqi liu on 14 Oct 2021
sir,please check the follow code to get some information
ImageFolder = "D:\sys2\New folder\8PSK\8PSK"
imds = imageDatastore('D:\sys2\New folder\8PSK\*.png') % Whatever...
numberOfImages = length(imds.Files)
for k = 1 : numberOfImages
% Get the input filename. It already has the folder prepended so we don't need to worry about that.
inputFileName = imds.Files{k};
fprintf('Checking %s\n', inputFileName);
rgbImage = imread(inputFileName);
grayImage = rgb2gray(rgbImage);
imshow(grayImage);
file_name = sprintf('Image%d.png', k)% name Image with a sequence of number, ex Image1.png , Image2.png....
fullFileName = fullfile(ImageFolder, file_name);
imgName = fullfile(ImageFolder,['Image_',num2str(k),'.png']);
disp(imgName);
[fd,~,~] = fileparts(imgName);
if ~exist(fd)
mkdir(fd);
end
imwrite(grayImage,imgName) ;
axis('on', 'image');
drawnow;
pause(0.1); % Short delay so we can see the image a little bit.
end
msgbox('Done!');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!