from PIL import Image import os import re import tqdm def create_grid(directory, save_path=None): # Get list of image files in the directory files = sorted([f for f in os.listdir(directory) if f.endswith('.png')]) # Assuming all images are the same size img_sample = Image.open(os.path.join(directory, files[0])) width, height = img_sample.size # Calculate grid dimensions for 24 images grid_width = width * 6 # 6 images in each row grid_height = height * 4 # 4 rows # Create new image for the grid grid_img = Image.new('RGB', (grid_width, grid_height)) for idx, file in enumerate(files): img = Image.open(os.path.join(directory, file)) x = idx % 6 * width # 6 images in each row y = idx // 6 * height # 4 rows grid_img.paste(img, (x, y)) if save_path: grid_img.save(f'{save_path}/{directory.split("/")[-1]}_grid.png') else: grid_img.save(f'{directory}_grid.png') def list_subdirectories(parent_directory): # Regex pattern to match the subdirectory naming convention pattern = re.compile(r"\d+_of_\d+_masked1") # List all subdirectories subdirs = [d for d in os.listdir(parent_directory) if os.path.isdir(os.path.join(parent_directory, d))] # Filter subdirectories based on naming convention matching_subdirs = [d for d in subdirs if pattern.match(d)] return matching_subdirs # List of directories # for prompt in ["A cat walking in a park", "A dog running in a park", " A wooden barrel drifting on a river", "A kite flying in the sky"]: # for prompt in ["A car driving on the road"]: # try: # directories = list_subdirectories(f"demo4/video41-att_mask/{prompt}") # except FileNotFoundError: # print(f"Directory not found: {prompt}") # continue # os.makedirs(f"demo4/{prompt}/consolidated_grids", exist_ok=True) # for directory in tqdm.tqdm(directories): # create_grid(os.path.join(f"demo4/video41-att_mask/{prompt}", directory), save_path=f"demo4/{prompt}/consolidated_grids")