-1

I want to do the following thing:

Get all the images in a directory (include the images in subdirectory), do something with them (for example: resize), then export these images to another directory with the same hierarchy.

For example:

I want to get images from input/flower/rose, resize it and export to output, ask python to automatically create the hierarchy flower/rose inside output.

How can I do that?

baduker
  • 19,152
  • 9
  • 33
  • 56
  • What have you tried that didn't work ? – bruno desthuilliers Mar 15 '18 at 11:07
  • first I used os.walk to get the hierarchy inside input, then use os.mkdir to creare the same hierarchy inside output, but it didnt work because there are some images with the same directory, which lead to the error "directory already exist" – Le Trong Nghia Mar 15 '18 at 11:12
  • Please edit your post to add your existing code and the complete error and traceback (cf https://stackoverflow.com/help/mcve). FWIW, handling this kind of errors is not really complicated . – bruno desthuilliers Mar 15 '18 at 11:50

1 Answers1

1

os.makedirs will recreate the directory hierarchy if you use e.g.

os.makedirs("output/flower/rose")
0x01
  • 468
  • 2
  • 9
  • I know it will, but actually in my task the images are stored in many folder, form a really complicated hierarchy, not only one like the example I mentioned. Also, the makedir will raise error if the directory already exist, so it is hard to use. – Le Trong Nghia Mar 15 '18 at 10:52
  • 1
    Regarding the "will raise an error", have a look at the top answers over here: https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist – 0x01 Mar 15 '18 at 11:23