Message Boards Message Boards

0
|
3493 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to write conventional looking programs in Mathematica

Posted 9 years ago

I've got lots and lots of digital photos. I would like to try and run ImageIdentify on them to see if it can generate useful keywords.

I'm just starting with Mathematica and I don't see how to write a conventional program of the form do i = 1 to 3 filepath = directory & filenames[[i]] ImageIdentify[Import[filepath]] next

Basically I don't want to have to force all the parts of the do to be on one line which seems to be the default. To be honest this formatting reminds me of when I had to program in Forth; very painful.

Thanks for any help!

POSTED BY: trinko
4 Replies

Hi there,

I don't think that you really want to do that - I mean writing it with a standard do or for loop. You can and it looks like that:

names = FileNames["~/Desktop/Images/*"]; 
list = {}; Do[AppendTo[list, {Import[names[[i]]], ImageIdentify[Import[names[[i]]]]}], {i, 1,Length[names]}]; 
Grid[list, Frame -> All]

assuming your files are in a folder called "~/Desktop/Images/". The first line gets all the file names. The second creates and empty list. The Do loop makes a list of images and their identification. The last command plots.

Mathematica is much more elegant than that. You can use functional programming - rather than the procedural style above. The functional version looks like this:

names = FileNames["~/Desktop/Images/*"]; 
Grid[{Import[#], ImageIdentify[Import[#]]} & /@ names, Frame -> All]

Both give the same output.

enter image description here

Cheers,

Marco

PS: If your images are in nested directories this would work:

names = FileNames["*.jpeg", "~/Desktop/Images/", Infinity]
POSTED BY: Marco Thiel
Posted 9 years ago

Perhaps

filenames = {"file1.jpg", "file2.jpg", "file3.jpg"};
For[i = 1, i <= 3, i++,
 filepath = "c:/yourPath/" <> filenames[[i]]; 
 Print[ImageIdentify[Import[filepath]]]
 ]

or

filenames = {"file1.jpg", "file2.jpg", "file3.jpg"};
Do[
 filepath = "c:/yourPath/" <> filenames[[i]]; 
 Print[ImageIdentify[Import[filepath]]],
 {i, 3}
 ]

or slightly less conventionally

filenames = {"file1.jpg", "file2.jpg", "file3.jpg"};
Table[
 filepath = "c:/yourPath/" <> filenames[[i]]; 
 ImageIdentify[Import[filepath]],
 {i, 3}
 ]

or even less conventionally

filenames = {"file1.jpg", "file2.jpg", "file3.jpg"};
Map[(
   filepath = "c:/yourPath/" <> #; 
   ImageIdentify[Import[filepath]]) &,
 filenames
 ]
POSTED BY: Bill Simpson
Posted 9 years ago

Thanks. The sad reality is that I've been programming for 43 years and I'm just more comfortable with standard procedural coding. If I wasn't doing this for fun I'd knuckle down and learn the new approach but my short term objective is just to be able to code quickly which in my case means using code that is easier for me to read.

The other issue I have is that it's not obvious to me how to generalize the code you showed for dealing with nested directories.

POSTED BY: trinko
Posted 9 years ago

Thanks! This is sort of what I was looking for.

POSTED BY: trinko
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