How to prevent image that has been stored not overwrite using imwrite?
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 14 de Jun. de 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 comentarios
Alvindra Pratama
el 14 de Jun. de 2016
Editada: Alvindra Pratama
el 14 de Jun. de 2016
Walter Roberson
el 14 de Jun. de 2016
You do not appear to be updating the current axes in the loop, so getframe() is going to fetch the same image each time.
Why are you fetching ImageFiles = dir(filePattern) when all you do with that information is use the number of entries as the loop index?
Alvindra Pratama
el 14 de Jun. de 2016
Walter Roberson
el 14 de Jun. de 2016
Why are you using dir() for the for looping? Why are you saving a different number of frames depending on the number of files that already exist in the directory, even though you do not care whether you overwrite any of those files? It would make some sense if you were choosing to overwrite each of the existing files, but you are not. It would also make some sense if you need to read the image files from one place and want to re-save them as numbered files, which does not even require displaying them.
Alvindra Pratama
el 14 de Jun. de 2016
Alvindra Pratama
el 14 de Jun. de 2016
natanael correia
el 1 de Sept. de 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!
Categorías
Más información sobre Image Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!