Problem of imwrite permission

5 views (last 30 days)
Hi!, recently I have been troubled with getting cropped picture for given input. What I expected are from input PolyU_0034_F.bmp will become crop_0034_F.jpg for example the code are:
file = dir('C:\Program Files\MATLAB\R2017a\bin\FYP\PolyU\*.bmp');
input = {file.name}';
c = input;
for i = 1:length(file)
fname = strcat('C:\Program Files\MATLAB\R2017a\bin\FYP\PolyU\',file(i).name);
a = imread(fname);
b = imcrop(a,[140,65,150,150]);
c{i} = strrep(fname, 'PolyU', 'crop');
d = c{i};
imwrite(b,strrep(d, '.bmp', '.jpg'));
imshow(b);
end
However, The error given out are
Error using imwrite (line 454)
Unable to open file "C:\Program Files\MATLAB\R2017a\bin\FYP\crop\crop_037_F_10.jpg" for writing. You might not have write
permission.
Error in corp1 (line 24)
imwrite(b,strrep(d, '.bmp', '.jpg'));
This is weird for 2 reasons. First, the file direction on error is different folder (crop) even though I use folder of (PolyU). Second, This code was done on different folder(FYP) and execute very well. Are same code in different folder need to readjust so that it could work again?
Thanks for your time!
  2 Comments
Walter Roberson
Walter Roberson on 19 Mar 2018
"First, the file direction on error is different folder (crop) even though I use folder of (PolyU)."
No you do not. You strrep(fname, 'PolyU, 'crop') to replace PolyU with crop, and then you use that as the destination for imwrite, so it is expected that you write to the crop directory.
Muhammad Bariq Azmi
Muhammad Bariq Azmi on 19 Mar 2018
Yeah, I don't realized about the path before also changed. Thanks for help!!!

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 2 Mar 2018
In general trying to write files in a directory under the MATLAB root directory is probably not a good idea, and in this case the error indicates you don't have write permissions to that directory and/or that file.
I strongly recommend moving your FYP subdirectory somewhere else that is NOT under "C:\Program Files\MATLAB\R2017a".
  3 Comments
Muhammad Bariq Azmi
Muhammad Bariq Azmi on 4 Mar 2018
Edited: Muhammad Bariq Azmi on 4 Mar 2018
Sorry sir, I still have error of write permission. This time, the error point to C:\FYP\work\FYP\crop\crop_037_F_10.jpg. The folder that i keep input picture are also different from error (C:\FYP\work\FYP\PolyU)

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!