MATLAB Answers

resizing all images in a folder

96 views (last 30 days)
Victoria Lawlor
Victoria Lawlor on 28 Sep 2015
Commented: Jon on 29 Sep 2015
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!

Accepted Answer

Jon
Jon on 28 Sep 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 Comments
Jon
Jon on 29 Sep 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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 Sep 2015
See the FAQ for code to do this:
Put your calls to imread(), imresize() and imwrite() inside the loop.
  1 Comment
Victoria Lawlor
Victoria Lawlor on 28 Sep 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!