imwrite does not work when file extension is given
4 views (last 30 days)
Show older comments
I am trying to create a folder of images using imwrite in a for loop. All previous problems with imwrite seem to stem from a permission problem or incorrect file naming. I have ensured my file permissions are correct in windows and I cannot find any error in my file naming. I first create a folder path using mkdir. Then, within a loop I define my image and its name, then save it using imwrite. Within imwrite I use fullfile(path,name) to create a correct file name at the location I want.
Here is my simple code
%% Greyscale shrinkage control structures
gval = 0.5;
grey = repelem(gval,95);
grey = uint8(grey*255);
wc = 608/2;
hc = 684/2;
w = 100;
h = 200;
path = fullfile(pwd,'grey_beams',['grey_shrinkage_control_' 'grey_val_' strrep(num2str(gval),'.','-')]);
mkdir(path)
for i = 1:96
I = uint8(zeros(684,608));
if i > 3 && i < 96
I(hc-h:hc+h,wc:wc+w) = 255;
I(hc-h:hc+h,wc-w:wc-1) = grey(i);
elseif i <= 3
I(hc-h:hc+h,wc-250:wc+250) = 255;
end
name = ['pattern_' sprintf('%03d',i-1) '.bmp']
imwrite(I,fullfile(path,name),'bmp')
end
This gives me the following error for the first iteration of the loop
% Error using imwrite (line 548)
% Unable to open file "C:\Users\Jason Johnson\Documents\Purdue\Purdue_Research\Polymerization
% Model\Code\Spatiotemporal Code\grey_beams\grey_shrinkage_control_grey_val_0-5\pattern_000.bmp" for
% writing. You might not have write permission.
%
% Error in grey_shrinkage_tuning (line 26)
% imwrite(I,fullfile(path,name),'bmp')
This code works perfectly when I delete '.bmp' from name on line 25. But, then it saves the files with no particular format, when, instead, I need them to be a bitmap.
Furthermore, the given code will work if I insert
edit(fullfile(path,name))
before the imwrite on line 26. However, then the code is slowed down significantly, and I have 96 tabs in the editor window that I have to close manually.
What is causing this file permission error?
5 Comments
dpb
on 13 Jul 2021
Edited: dpb
on 13 Jul 2021
The deal about only the extension being on the filename causing the problem convinces me it is something of the sort if you can otherwise access the subdirectory.
It isn't imwrite that's the culprit if fopen() also has the problem--that's tied in to the OS and file system and permissions for the specific file(s).
Accepted Answer
Jan
on 13 Jul 2021
Edited: Jan
on 13 Jul 2021
Do not use "path" as name of a variable, because this shadows an important Matlab command. This does run usually, but during debugging you can get serious troubles.
Relying on this current directory is fragile, because any callback of a timer or GUI can change it unexpectedly. So instead of useing pwd() (which does nothing than callin cd() by the way), provide an absolute path.
"then it saves the files with no particular format" - there are no files without a format. imwrite uses the format from the file name, if you omit the specifier 'bmp' to determine the format.
Use this code for checking the existence of the file:
gval = 0.5;
grey = repmat(uint8(gval * 255), 1, 95);
wc = 608/2;
hc = 684/2;
w = 100;
h = 200;
base = 'C:\Your\Base\Path';
folder = fullfile(base, 'grey_beams', ...
['grey_shrinkage_control_grey_val_', strrep(num2str(gval), '.', '-')]);
mkdir(folder)
for i = 1:96
I = zeros(684,608, 'uint8');
if i <= 3
I(hc-h:hc+h, wc-250:wc+250) = 255;
elseif i < 96
I(hc-h:hc+h, wc:wc+w) = 255;
I(hc-h:hc+h, wc-w:wc-1) = grey(i);
end
file = fullfile(folder, sprintf('pattern_%03d.bmp', i-1));
if isfile(file)
error('File is existing already: %s', file);
end
imwrite(I, file, 'bmp');
% ^^^^^ this is optional
end
What do you observe?
I've inserted some simplifications in the code.
8 Comments
More Answers (2)
Image Analyst
on 14 Jul 2021
I ran the original code and it ran fine - no errors. Nonetheless, I made some improvements in it:
fprintf('Beginning to run %s.m ...\n', mfilename);
%% Greyscale shrinkage control structures
gval = 0.5;
grey = repelem(gval,95);
grey = uint8(grey*255);
wc = 608/2;
hc = 684/2;
w = 100;
h = 200;
folder = fullfile(pwd,'grey_beams', ['grey_shrinkage_control_', 'grey_val_', strrep(num2str(gval),'.','-')]);
if ~isfolder(folder)
mkdir(folder);
end
for k = 1 : 96
grayImage = uint8(zeros(684,608));
if k > 3 && k < 96
grayImage(hc-h:hc+h,wc:wc+w) = 255;
grayImage(hc-h:hc+h,wc-w:wc-1) = grey(k);
elseif k <= 3
grayImage(hc-h:hc+h,wc-250:wc+250) = 255;
end
baseFileName = sprintf('pattern_%03d.bmp', k-1);
fullFileName = fullfile(folder, baseFileName);
fprintf('Writing %s\n', fullFileName);
imwrite(grayImage, fullFileName);
end
fprintf('Done running %s.m.\n', mfilename);
What if you try using PNG images? PNG is a much more common format these days than BMP.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!