M Terry
M Terry

Reputation: 183

How can I process images from nested directories and save them into their respective directories in python?

I have been trying to resize all images contained in nested directories and save the resulting image into directories with the same structure as the original one. I keep getting the error that the directory or file doesn´t exist (though it does really exist).

root_path= 'D:/Users/mbeng/OneDrive/Desktop/mass_buildings'
def locate(pattern, root_path):
    for path, dirs, files in os.walk(os.path.abspath(root_path)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

path = [f for f in locate('*.tiff', root_path)]

for file in path:
    i = Image.open(file)

    #fname = file[file.find('mass_buildings\\'):]
    fname = file.replace('D:\\Users\\mbeng\\OneDrive\\Desktop\\mass_buildings', 'D:\\Users\\mbeng\\OneDrive\\Desktop\\resized2')
    #fname = fname.replace('\\', '_')

    fn, fext = os.path.splitext(fname)
    #print(file)
    img = i.resize((300, 300))
    #print(img)
    img.save('{}.tiff'.format(fn), 'TIFF')

when I run the above code, I get the error:

D:\Users\mbeng\Python\PyTorch\python.exe D:/Users/mbeng/Python/FeatureExtract/fils_list.py
Traceback (most recent call last):
  File "D:/Users/mbeng/Python/FeatureExtract/fils_list.py", line 68, in <module>
    img.save('{}.tiff'.format(fn), 'TIFF')
  File "D:\Users\mbeng\Python\PyTorch\lib\site-packages\PIL\Image.py", line 2085, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Users\\mbeng\\OneDrive\\Desktop\\resized2\\test\\map\\22828930_15.tiff'

Process finished with exit code 1

reized2 is the directory i created to save the processed files in. it contains the directories: train, test and valid, each containing two subdirectories: sat and map. The directory of the original (mass_buildings) files have the same structure as resized2. How can I make it work?

Upvotes: 0

Views: 358

Answers (1)

M Terry
M Terry

Reputation: 183

I figured out a couple of things that were making the code not to work.

  1. the replace() method wasn´t working, so I changed the paths to raw strings and replaced the // with \.

  2. I had to delete the pre-created directory for saving the processed files so that they are created at runtime using Pathlib.Path().mkdir().

root_path= 'D:/Users/mbeng/OneDrive/Desktop/mass_buildings'
def locate(pattern, root_path):
    for path, dirs, files in os.walk(os.path.abspath(root_path)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

path = [f for f in locate('*.tiff', root_path)]

for file in path:
    fname = file.replace(r'D:\Users\mbeng\OneDrive\Desktop\mass_buildings', r'D:\\Users\\mbeng\\OneDrive\\Desktop\\resized')
    fp = os.path.split(fname)[:-1][0]
    base = os.path.basename(fname)
    Path(fp).mkdir(parents=True, exist_ok=True)
    fn, fext = os.path.splitext(base)

    i = Image.open(file)
    img = i.resize((700, 700), PIL.Image.NEAREST)
    img.save(os.path.join(fp, '{}.tiff'.format(fn)))

Upvotes: 1

Related Questions