1 | initial version |
To retrieve the size and path of images located in a directory, excluding specific subfolders, using recursion, we can use the following steps:
Here is a sample code:
import os from PIL import Image
def getimagesizes(path, excludedfolders=[]): files = os.listdir(path) imagesizes = [] for file in files: filepath = os.path.join(path, file) if os.path.isdir(filepath) and file not in excludedfolders: # recursively call the function for subdirectories imagesizes.extend(getimagesizes(filepath, excludedfolders)) elif os.path.isfile(filepath) and file.lower().endswith(('.jpg', '.jpeg', '.png')): try: img = Image.open(filepath) imagesizes.append({ 'path': filepath, 'size': img.size }) img.close() except: pass return image_sizes
path = '/path/to/directory' excludedfolders = ['subfolder1', 'subfolder2'] imagesizes = getimagesizes(path, excludedfolders) print(imagesizes)