resizing all images in a folder

18 visualizaciones (últimos 30 días)
Victoria Lawlor
Victoria Lawlor el 28 de Sept. de 2015
Comentada: Image Analyst el 26 de Mzo. de 2024
I am able to get matlab to resize and save an individual image, but I'm wondering how I can get it to do this for all images in a folder without having to type in the names one by one. The names of the saved & original images must stay the same if that makes any difference. Could I load the images into an array and then have it loop through each image? Sorry if this is a dumb question!

Respuesta aceptada

Jon
Jon el 28 de Sept. de 2015
Yep. Try the following function:
dc = dir('*.jpg');
but set the filetype to whatever the images are. Make sure your working directory is the one with the images, obviously. The 'dc' variable will be structure that contains the file information, which will allow you to automate the loading, resizing, and saving of each image.
  10 comentarios
Image Analyst
Image Analyst el 29 de Sept. de 2015
For example, let's say your images were in C:\whatever, and your m-files were in C:\m-files. Now if you ran the script and called cd('c:\whatever') and then tried to call resize_and_save() function, it would not find it (unless you had made sure that folder was on the path) because the current folder is no longer C:\m-files.
I literally have hundreds of projects each in their own folder (because they're unrelated). I do not want to clutter my path with hundreds of folders, some of which I rarely use anymore, so usually I do not add my folder to the search path.
Jon
Jon el 29 de Sept. de 2015
I see. I've just been doing
cd('c:\whatever')
load data
cd('working_path')
I guess it's a little uglier and has the potential for problems if I'm running code line-by-line.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 28 de Sept. de 2015
See the FAQ for code to do this:
Put your calls to imread(), imresize() and imwrite() inside the loop.
  1 comentario
Victoria Lawlor
Victoria Lawlor el 28 de Sept. de 2015
Like I mentioned before I haven't used matlab very much. I understand how I would load the whole file but I'm still not sure how I would have it go through each image individually. If the dir function loads the images into an array then maybe the issue is more that I don't understand how to go through each part of an array and perform some action.
I'm not sure what arguments I would put in the imread, imresize, and imwrite functions since before I had the image name.

Iniciar sesión para comentar.


anis martina
anis martina el 26 de Mzo. de 2024
Editada: anis martina el 26 de Mzo. de 2024
resize multiple image using in matlab image import in file code
  1 comentario
Image Analyst
Image Analyst el 26 de Mzo. de 2024
See the attached demo. It computes the average RGB image. If the second and subsequent images are of a different size than the first image, it will resize them to match the first image. You can easily not sum the images (because you don't need the mean) and add a call to imwrite to save them to disk, just create the full file name using fullfile and put them in different folder just in case you have some kind of mistake that you don't destroy the original.

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by