from PIL import Image import io import base64 def crop_face(image_bytes, bbox): image = Image.open(io.BytesIO(image_bytes)) width, height = image.size ymin, xmin, ymax, xmax = bbox x1 = int(xmin / 1000 * width) y1 = int(ymin / 1000 * height) x2 = int(xmax / 1000 * width) y2 = int(ymax / 1000 * height) face = image.crop((x1, y1, x2, y2)) buffer = io.BytesIO() face.save(buffer, format="JPEG") return base64.b64encode(buffer.getvalue()).decode()