Multiple Import and Export of Images

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”.

=================

=================

1 Answer
1

=================

Let your images lie in /pathtoimages/unchanged/.

Create the folder /pathtoimages/changed/

SetDirectory[“/pathtoimages/unchanged/”];
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

  

 

@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.
– LLlAMnYP
Oct 21 ’15 at 22:23

  

 

@Karsten7. unfortunately, I’m not at the same machine I was at at the time of writing the answer.
– LLlAMnYP
Oct 21 ’15 at 22:36

  

 

@ 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
– Felicitas
Oct 23 ’15 at 8:05

  

 

@Karsten7. (though the comments have now been deleted), yes, ParallelDo gave a 2x speed-up.
– LLlAMnYP
Oct 23 ’15 at 8:30