import os

def extract_palette(input_folder):
    # Check whether the input folder exists
    if not os.path.isdir(input_folder):
        print(f"Error: Folder '{input_folder}' does not exist.")
        return

    # Get list of files in the input folder
    files = os.listdir(input_folder)

    # Process each file
    for file in files:
        # Check if file ends with "1.raw"
        if file.endswith("1.raw"):
            # Build full path to the file
            file_path = os.path.join(input_folder, file)

            # Extract bytes from 0x10 to 0x310
            with open(file_path, 'rb') as f:
                f.seek(0x10)
                palette_bytes = f.read(0x300)

            # Create .pal filename
            output_pal_name = file[:9] + ".pal"

            # Save extracted bytes to .pal file
            with open(output_pal_name, 'wb') as f:
                f.write(palette_bytes)

            print(f"Created '{output_pal_name}' with palette extracted from '{file}'.")

# Specify input folder
input_folder = "images"

# Run palette extraction
extract_palette(input_folder)