I wrote a program that imports an image and creates three new images, for example "black and white" or "grey scaled".

My problem is that i work with around 200 images and it takes a long time to import the images manually, and then to save the three created images manually.

Is it possible to create a loop that imports the images and exports the three new images with a changed name to a specific folder?

For example: I import the image "holiday" from the folder "Unchanged" and export "holiday_BlackWhite" or "holiday_greyscaled" to the folder "Changed".


Let your images lie in /pathtoimages/unchanged/.

Create the folder /pathtoimages/changed/

names = FileBaseName /@ FileNames["*.png"];

I'm using the extension png in my example, as I have a bunch of png images somewhere on my machine. If you have photos, they're most likely with the extension jpg - adjust accordingly.

Do[im = Import[name <> ".png"]; 
 Export["../changed/" <> name <> "_blackwhite.png", Binarize[im]];
 Export["../changed/" <> name <> "_grayscale.png", ColorConvert[im, "Grayscale"]];
 , {name, names}]

It's quite slow - took about 30 seconds for 11 files with a total size of 3MB

| improve this answer | |
  • $\begingroup$ @Karsten7. To be honest, I have no experience with the Parallel* functions.I guess a speed-up is possible, unless some collisions with the repeated assignment happen. $\endgroup$ – LLlAMnYP Oct 21 '15 at 22:23
  • $\begingroup$ @Karsten7. unfortunately, I'm not at the same machine I was at at the time of writing the answer. $\endgroup$ – LLlAMnYP Oct 21 '15 at 22:36
  • $\begingroup$ @ LLlAMnYP. Thank you for your quick answer. To be honest, my programm a lot more complex and i used the example "holiday" just to show what kind of problem exists. But your answer gave me the right thought-provoking impulse. Thanks $\endgroup$ – Felicitas Oct 23 '15 at 8:05
  • $\begingroup$ @Karsten7. (though the comments have now been deleted), yes, ParallelDo gave a 2x speed-up. $\endgroup$ – LLlAMnYP Oct 23 '15 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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