import os
import random
from PIL import Image


def rotate_images_random_angle(input_folder, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for filename in os.listdir(input_folder):
        if filename.lower().endswith('.png'):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, filename)
            angle = random.uniform(0, 360)
            try:
                with Image.open(input_path) as img:
                    rotated_img = img.rotate(angle, expand=True)
                    rotated_img.save(output_path)
                    print(f"Rotated {filename} by {angle:.2f} degrees.")
            except Exception as e:
                print(f"Error processing {filename}: {e}")


if __name__ == "__main__":
    input_folder = 'output'
    output_folder = 'output2'
    rotate_images_random_angle(input_folder, output_folder)
