import os
from PIL import Image

def resize_image(image_path, max_width, max_height):
    try:
        image = Image.open(image_path)

        width, height = image.size

        if width > max_width or height > max_height:
            aspect_ratio = width / height

            if width > max_width:
                new_width = max_width
                new_height = int(new_width / aspect_ratio)
            else:
                new_height = max_height
                new_width = int(new_height * aspect_ratio)

            resized_image = image.resize(new_width, new_height)

            resized_image.save(image_path)
            print("Image resized successfully:", image_path)
        else:
            print("Image dimensions are within the desired limits:", image_path)
    except Exception as e:
        print('failed', e)

def process_images_in_folder(folder_path, max_width, max_height):
    for root, _, files in os.walk(folder_path):
        for file_name in files:
            if file_name.lower().endswith((".jpg", ".jpeg", ".png", ".bmp", ".gif")):
                image_path = os.path.join(root, file_name)

                resize_image(image_path, max_width, max_height)

folder_path = input('Path to folder:')

max_width = 720
max_height = 1280

process_images_in_folder(folder_path, max_width, max_height)