import os
from PIL import Image

def convert_indexed_to_rgb(parent_folder):
    # Loop through all files in the specified folder and its subfolders
    for root, dirs, files in os.walk(parent_folder):
        for file in files:
            file_path = os.path.join(root, file)
            try:
                with Image.open(file_path) as img:
                    # Check if the image is in indexed color mode
                    if img.mode == 'P':
                        # Convert the image to RGB mode
                        img = img.convert('RGB')
                        # Save the converted image back to the same path
                        img.save(file_path)
                        print(f"Converted {file_path} to RGB mode.")
            except Exception as e:
                print(f"Failed to convert {file_path}: {e}")

# Specify the parent folder path
parent_folder = 'bed'

# Call the function to convert images
convert_indexed_to_rgb(parent_folder)
