How to prevent image that has been stored not overwrite using imwrite?

14 次查看(过去 30 天)
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 :
And the i try to save the 2nd image, but it is replace my first image become like this :
How can i prevent that overwrite my saved images but still save the next image with continue numbering filename?

采纳的回答

Image Analyst
Image Analyst 2016-6-14
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 个评论
Alvindra Pratama
Alvindra Pratama 2016-6-14
编辑:Image Analyst 2016-6-14
Here my new code :
folder = 'H:\SKRIPSI\Citra Latih 2\';
ImageFiles = dir(folder);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
imwrite(img.cdata,fullfile(folder,baseFileName));
end
The code above can save images in increment filename like 1.jpg, 2.jpg, 3.jpg and so on but still overwrite the previous images with the new images. What is wrong with my code? Can you help me to fix it?
Image Analyst
Image Analyst 2016-6-14
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-6-14
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 个评论
Alvindra Pratama
Alvindra Pratama 2016-6-14
Here my new code :
folder = 'H:\SKRIPSI\Citra Latih 2\';
ImageFiles = dir(folder);
a = length(ImageFiles)-1;
for Index = 1:a
baseFileName = [num2str(Index),'.jpg'];
imwrite(img.cdata,fullfile(folder,baseFileName));
end
the code above can save images in increment filename like 1.jpg, 2.jpg, 3.jpg and so on but still overwrite the previous images with the new images. What is worng with my code? can you help me to fix it?
natanael correia
natanael correia 2020-9-1
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by