imwrite does not work when file extension is given
Ältere Kommentare anzeigen
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 Kommentare
dpb
am 13 Jul. 2021
You have opened a file of that name previously and not closed it would be my hypothesis...ensure the target directory is empty first and you haven't any open in some other application.
Jason Johnson
am 13 Jul. 2021
Simon Chan
am 13 Jul. 2021
Seems you have already had the files with same file names in the directory and they are set to read only.
Jason Johnson
am 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).
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 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.
1 Kommentar
Jason Johnson
am 14 Jul. 2021
Jason Johnson
am 14 Jul. 2021
0 Stimmen
Kategorien
Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!