""" Image Compression Script for mudassirishfaq.com Run: python compress-images.py Requires: pip install Pillow """ from PIL import Image import os ASSETS_DIR = os.path.join(os.path.dirname(__file__), "assets") MAX_WIDTH = 1400 # max width in pixels JPEG_QUALITY = 82 # 80-85 is sweet spot: high quality, small file PNG_COMPRESS = 7 # 0-9, higher = smaller file but slower def compress_image(path): original_size = os.path.getsize(path) img = Image.open(path) # Downscale if wider than MAX_WIDTH if img.width > MAX_WIDTH: ratio = MAX_WIDTH / img.width new_h = int(img.height * ratio) img = img.resize((MAX_WIDTH, new_h), Image.LANCZOS) ext = os.path.splitext(path)[1].lower() if ext in (".jpg", ".jpeg"): # Convert RGBA to RGB if needed if img.mode in ("RGBA", "P"): img = img.convert("RGB") img.save(path, "JPEG", quality=JPEG_QUALITY, optimize=True) elif ext == ".png": img.save(path, "PNG", compress_level=PNG_COMPRESS, optimize=True) new_size = os.path.getsize(path) saved = (1 - new_size / original_size) * 100 print(f" {os.path.basename(path)}: {original_size//1024}KB → {new_size//1024}KB ({saved:.0f}% smaller)") print("Compressing images in assets/ ...\n") for filename in os.listdir(ASSETS_DIR): if filename.lower().endswith((".jpg", ".jpeg", ".png")): compress_image(os.path.join(ASSETS_DIR, filename)) print("\nDone! Re-upload the assets/ folder to Hostinger.")