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".
Answer
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
Comments
Post a Comment