I am using Mathematica for the processing of images. For this I have to process a lot of images. Currently, I import one by one of the images to run the simulation. I want to import all the image files of a folder and assign variable to each of them...How to do it? Any suggestion?

  • 1
    $\begingroup$ Welcome to mma.se! Please consider setting a username, it might even get you an upvote or two :P $\endgroup$ – Jacob Akkerboom Mar 10 '14 at 9:39
images = FileNames["*.jpg"];
count = 1
Do[imagevar[count++] = Import[image], {image, images}]

Images will be in imagevar[1], imagevar[2]... so you can easily index into them.

Obviously, change directory / filenames to suit.


If you have numbered or desired variable names in mind (acknowledging there are number padding issues for large numbers) you could use. Assuming you have set working directory as per rasher:

images = FileNames["*.jpg"];
 Set[#1, #2] &, {ToExpression /@ 
   Table["img" <> 
     ToString@NumberForm[j, 3, NumberPadding -> {"0", ""}], {j,Length[images]}], 

In this case img0001 is set to images[[1]], img0002 is set to images[[2]],...

However, using an array (as per rasher) will be much easier to deal with and refer to for all but the smallest sets. Otherwise just import the image files into one variable and choose relevant parts of interest.


Not the answer you're looking for? Browse other questions tagged or ask your own question.