#!/usr/bin/env python3 import sys from PIL import Image source, target, cap, quality = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]) with Image.open(source) as image: image = image.convert('RGB') if len(sys.argv) == 9: x, y, width, height = [int(v) for v in sys.argv[5:9]] image = image.crop((x, y, x + width, y + height)) scale = min(1.0, cap / max(image.width, image.height)) if scale < 1.0: image = image.resize((round(image.width * scale), round(image.height * scale)), Image.Resampling.LANCZOS) image.save(target, 'WEBP', quality=quality, method=4)