from PIL import Image
import os

for f in os.listdir('.'):
    if f.lower().endswith(('.jpg','.jpeg','.png')):
        img = Image.open(f)
        w, h = img.size
        if min(w, h) < 500:
            s = 800 / min(w, h)
            img = img.resize((round(w*s), round(h*s)), Image.LANCZOS)
            os.makedirs('optimised', exist_ok=True)
            img.save('optimised/' + f)
            print(f'{f}: {w}x{h} -> {img.size}')
        else:
            print(f'{f}: {w}x{h} OK')
