How to prevent image that has been stored not overwrite using imwrite?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alvindra Pratama
am 14 Jun. 2016
Kommentiert: natanael correia
am 1 Sep. 2020
I want to save an image from current axes directly to a folder in my computer using this code :
img = getframe(gca);
folder = 'H:\SKRIPSI\Citra Latih 2\';
filePattern = fullfile(folder, '/*.*');
ImageFiles = dir(filePattern);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
filename = fullfile(folder, baseFileName);
imwrite(img.cdata,fullfile(filename));
When i save the first image, it could be like this :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153869/image.png)
And the i try to save the 2nd image, but it is replace my first image become like this :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153871/image.png)
How can i prevent that overwrite my saved images but still save the next image with continue numbering filename?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 14 Jun. 2016
Like Walter said, you're not changing img inside your loop so it's always the same. As an extra precaution against overwriting, use the exist() function
if exist(filename, 'file')
promptMessage = sprintf('%s already exists.\nDo you want to overwrite it', filename);
titleBarCaption = 'Overwrite?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(buttonText, 'Yes')
imwrite(img.cdata,filename);
end
else
imwrite(img.cdata,filename);
end
Note that filename is already the full path so you don't need to send it into fullfile again when you call imwrite().
3 Kommentare
Image Analyst
am 14 Jun. 2016
Your reluctance to use the extra safety offered by exist() is truly baffling.
imwrite() will be default overwrite the file if a file with that same name already exists. If you don't want that to happen, the only way around it is to detect if the file exists and then to take some action, such as (1) ask the user to overwrite or skip, (2) rename the file, either by asking user or automatically somehow, (3) save the file to a different folder that does not have a file with that name in it.
Also, remember you have to change img.cdata somehow - you omitted that code from what you've shown, so it will be the same on each iteration like we've already told you. Hopefully there's more code in the loop that, for some reason, you just omitted from what you've posted.
Weitere Antworten (1)
Walter Roberson
am 14 Jun. 2016
Your statement
imwrite(img.cdata,fullfile(filename));
is writing the same data to each of the files. You need to do the getframe() inside the loop, or else you have to get several frames outside of the save loop, storing the results in different locations, and then referencing the locations when you do the imwrite. For example,
for F = 1 : 10
img{F} = getframe();
end
for F = 1 : 10
filename = sprintf('%d.jpg', F);
imwrite(img{F}, filename);
end
Question: why are you checking the contents of the directory when you do not read files from the directory and you do not take care to ensure you are not overwriting anything in the directory?
7 Kommentare
natanael correia
am 1 Sep. 2020
man, just make a copy of the folder where the images are.
Then you run the program, it will replace all the images in the copied folder, then just throw the files together, windows will ask if you want to replace the files, why they will be in the same names, then you select who wants to keep both files,
READY!
automatically a huge amount of images will start to come together and you will have them all!
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!