Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a stack of images with the file names C0001996_0000 , C0001996_0001 etc. In total I have $600$ images. I would like to import the complete stack and then Binarize them (threshold 0.7) and then export them.

Could someone help me with the code for this?
At present I am importing a single image, binarizing it and then exporting.

share|improve this question

closed as off-topic by Ajasja, Artes, István Zachar, Kuba, m_goldberg Sep 9 '13 at 22:00

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – István Zachar, Kuba, m_goldberg
  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Ajasja, Artes
If this question can be reworded to fit the rules in the help center, please edit the question.

If you set the correct directory then you can: data = Import /@ FileNames["C0001996*"]. But I assume you want to do this step by step. So FileNames["C0001996*"] is a list of files. Import each one->binarize->export->import another. –  Kuba Sep 9 '13 at 17:59
Well, yes. That's what loops are for. If you are not comfrtable with Map or Scan you can use Do with procedures you want over the list of files. –  Kuba Sep 9 '13 at 18:18
I am completely new to Mathematica. Could you direct me to a video or handout on where I can read further into this. Ideally, I would want to import as a loop, Binarize them with the same threshold of 0.7, and then export and save them as '1.jpg, 2.jpg etc' rather than manually importing 600 images and saving them individually. –  Syed Sep 9 '13 at 18:31

1 Answer 1

Put your notebook to the directory of those pics or change NotebookDirectory[] part in the code so the direcotry is correct. Then:

files = FileNames["C0001996*"];

   pic = Import[files[[i]]];
   Export[ToString[i] <> ".jpg", Binarize[pic, 0.7]];
   {i, Length@files}]

I recomend to test this in the directory of for example 5 files :).

share|improve this answer
When i run the following code Do[pic = Import[files[[i]]]; Export[ToString[i] <> ".jpg", Binarize[pic, 0.7]];, {i, 1}] the error is Part;;partw ; Part 1 of {} does not exist. Import::chtype: "First argument {}[[1]] is not a valid file, directory, or URL specification. Binarize::imginv: Expecting an image or graphics instead of $Failed. –  Syed Sep 9 '13 at 19:00
Take away the semicolons for beginners...:) –  cormullion Sep 9 '13 at 19:14
But how else will they feel the awesome ****power**** of mathematica ? :) –  cormullion Sep 9 '13 at 19:41
Thanks, Its worked i think. Now I have 5 binary images saved in the same folder. Can i run the same code for more images? –  Syed Sep 10 '13 at 11:18
@Syed and Kuba, I have been notified by the system of the many comments here. I have given Syed Chat access. Please make use of it and clean up the comments here. –  Mr.Wizard Sep 10 '13 at 11:24

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