File size: 498 Bytes
d3bcdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()