Spaces:
Paused
Paused
File size: 1,179 Bytes
8d1819a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from PIL import Image
import io
import math
def compress_image(image_data: bytes, *, max_pixels: int = 256_000, quality: int = 50) -> bytes:
"""Compress an image by scaling it down and converting to JPEG with quality settings.
Args:
image_data: Raw image bytes
max_pixels: Maximum number of pixels in the output image (width * height)
quality: JPEG quality setting (1-100)
Returns:
Compressed image as bytes
"""
# load image from bytes
img = Image.open(io.BytesIO(image_data))
# calculate scaling factor to get to max_pixels
current_pixels = img.width * img.height
if current_pixels > max_pixels:
scale = math.sqrt(max_pixels / current_pixels)
new_width = int(img.width * scale)
new_height = int(img.height * scale)
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
# convert to RGB if needed (for JPEG)
if img.mode in ('RGBA', 'P'):
img = img.convert('RGB')
# save as JPEG with compression
output = io.BytesIO()
img.save(output, format='JPEG', quality=quality, optimize=True)
return output.getvalue()
|