Message Boards Message Boards

GROUPS:

Import a lot of images with Mathematica?

Posted 1 month ago
303 Views
|
3 Replies
|
0 Total Likes
|

Hi, I am a beginner at image precessing in Mathematica, I want to know how to import a lot of images by using for loop.

3 Replies
Posted 1 month ago

You do not use a for loop for this. This is a functional programing language that works best with recursive built in high level functions. Mathematica has a built in For loop but that should be used only in situations where you are teaching For loops in a beginner level computer programming concepts class. For loops are part of a strictly procedural programming language while bulit in high level functions in Mathematica are the big advantage to using a functional programming language. The Function call will rewrite itself when it is evaluated so that it will return the result of the call in place. When there is no assignment and no suppressed output, Mathematica will return the results to the screen as output using some other built in function that is most likely to be useful for the type of data being returned to the screen. For example sending a sound file to the screen will return an interactive sound player automatically.

Map[Import, FileNames["images/*.jpg", NotebookDirectory[]]]

this can also be written as

Import/@FileNames["images/*.jpg", NotebookDirectory[]]

will return the images as a list of images in the standard list format separating the elements of the list with commas.

{,,,,}

It will then display all the images on the screen unless there are a lot of them like 20,000 images for example. this would auto correct to suppressed output like what you would get with

Import/@FileNames["images/*.jpg", NotebookDirectory[]];

to load all the images to a list of images and store that list of images in variable a or list a

a = Import /@ FileNames["images/*.jpg", NotebookDirectory[]];

notice that this a is lowercase. this is important since all user functions in mathematica begin with a lowercase letter. all lists are also functions as well as expressions. don't worry about that too much. it will all make sense later.

ImageMultiply[#, 2]&/@ColorConvert[Import/@FileNames["images/*.jpg",NotebookDirectory[]],"Grayscale"]

notice we are using the built in function Map to perform Import[] on every element of the list. the result is

Table[Import[],{Length[FileNames["images/*.jpg",NotebookDirectory[]]}]

or

Import[];Import[];Import[];Import[];Import[];Import[]

it is hard to describe. maybe you can think of it like

{Import[],Import[],Import[],Import[],Import[],Import[]}

then you can figure out that

ColorConvert[Import/@FileNames["images/*.jpg",NotebookDirectory[]],"Grayscale"]

is actually

ColorConvert[{"flowers.jpg","sky.jpg","testpattern.jpg"},"Grayscale"]

therefore

ImageMultiply[#, 2]&/@ColorConvert[Import/@FileNames["images/*.jpg",NotebookDirectory[]],"Grayscale"]

is actually

ImageMultiply[ColorConvert["flowers.jpg","Grayscale"], 2]
ImageMultiply[ColorConvert["sky.jpg","Grayscale"], 2]
ImageMultiply[ColorConvert["testpattern.jpg","Grayscale"], 2]
Posted 1 month ago

Thank you John. I will work with this, if I faced\ any problem I will again post.

Posted 1 month ago

Hi John I have another question that how could I used image adjust command for multiple images. I did something once for one image, I will show that one but can you please help me how to do samething for multiple images.

Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract