File size: 1,243 Bytes
09abb1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import time
from PIL import Image


# Function to convert PIL image to binary
def pil_to_binary(img):
    with io.BytesIO() as output:
        img.save(output, format="PNG")
        return output.getvalue()


def binary_to_pil(binary):
    return Image.open(io.BytesIO(binary))


def crop_image(image_array, x, y, w, h):
    return image_array[y: y + h, x: x + w]


def extract_faces(image_one, image_two, results):
    image_one_results = results['facial_areas']['img1']
    cropped_image_one = crop_image(image_one, 
                                    image_one_results['x'],
                                    image_one_results['y'],
                                    image_one_results['w'],
                                    image_one_results['h'],)
    
    image_two_results = results['facial_areas']['img2']
    cropped_image_two = crop_image(image_two, 
                                    image_two_results['x'],
                                    image_two_results['y'],
                                    image_two_results['w'],
                                    image_two_results['h'],)
    
    return Image.fromarray(cropped_image_one).resize((480, 480)), Image.fromarray(cropped_image_two).resize((480, 480))