```python import pygame import os def create_spritesheet(image_paths, output_path, rows=1, padding=0): """ Combine multiple images into a single spritesheet. Args: image_paths (list): List of paths to source images output_path (str): Path to save the combined spritesheet rows (int): Number of rows in the spritesheet padding (int): Pixel padding between images """ if not image_paths: raise ValueError("No images provided") # Load all images images = [] max_width = 0 max_height = 0 for path in image_paths: try: img = pygame.image.load(path)#.convert_alpha() images.append(img) max_width = max(max_width, img.get_width()) max_height = max(max_height, img.get_height()) except pygame.error as e: print(f"Failed to load {path}: {e}") continue if not images: raise ValueError("No valid images loaded") # Calculate spritesheet dimensions cols = (len(images) + rows - 1) // rows sheet_width = cols * (max_width + padding) - padding sheet_height = rows * (max_height + padding) - padding # Create blank spritesheet surface spritesheet = pygame.Surface((sheet_width, sheet_height), pygame.SRCALPHA) # Blit images onto spritesheet for i, img in enumerate(images): row = i // cols col = i % cols x = col * (max_width + padding) y = row * (max_height + padding) spritesheet.blit(img, (x, y)) # Save the spritesheet pygame.image.save(spritesheet, output_path) print(f"Spritesheet saved to {output_path}") pygame.init() filenames = os.listdir("./tiles") os.chdir("tiles") # List of image paths to combine image_files = filenames cols=8 rows=len(image_files)//cols + 1 print(f"cols: {cols}, rows: {rows}") ## Create and save spritesheet create_spritesheet(image_files, "../spritesheet/combined_spritesheet.png", rows, padding=1) os.chdir("..") ## Create+Store metadata mapping = ["x_col,y_col,tilename"] for index, filename in enumerate(image_files): x_col = index // cols y_row = index - x_col * cols print(f"{index} - [{x_col},{y_row}] -> {filename}") tilename = filename.split(".")[0] mapping.append( f"{x_col},{y_row},{tilename}" ) print("Wrote spritesheet/combined_spritesheet.png") f = open("mapping.txt","w") f.write("\n".join(mapping)) f.close() ```