19 lines
820 B
Python
19 lines
820 B
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import json
|
|
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')
|
|
source_width, source_height = image.size
|
|
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)
|
|
|
|
print(json.dumps({'source_width': source_width, 'source_height': source_height, 'width': image.width, 'height': image.height, 'scale': scale}))
|