File size: 2,800 Bytes
4bec64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
from pathlib import Path
from PIL import Image
import numpy as np

def crop(img, point, mode=0, size=(512, 512), box=None, face_ratio=3, shreshold=1.5):
    img_width, img_height = img.size
    tgt_width, tgt_height = size
    point = (point[0]*img_width, point[1]*img_height)

    # mode 0 : automatic
    if mode == 0:
        if box is None:
            raise RuntimeError('face bax parameter expected: missing box=(width, height)')
        if img_width < tgt_width or img_height < tgt_height:
            mode = 1
        elif face_ratio ** 2 * shreshold ** 2 * box[0] * box[1] * img_width * img_height < tgt_width * tgt_height:
            mode = 2
        else:
            mode = 3

    # mode 1 : no scale
    if mode == 1:
        pass

    # mode 2 : full screen - crop as largr as possible
    if mode == 2:
        if tgt_width/img_width > tgt_height/img_height:
            r = tgt_height / tgt_width
            tgt_width = img_width
            tgt_height = round(tgt_width * r)
        else:
            r = tgt_width / tgt_height
            tgt_height = img_height
            tgt_width = round(tgt_height * r)  
    
    # mode 3 : fixed face ratio
    if mode == 3:
        if box is None:
            raise RuntimeError('face bax parameter expected: missing box=(width, height)')
        box_width = box[0] * img_height
        box_height = box[1] * img_height
        if box_width/tgt_width > box_height/tgt_width:
            r = tgt_height / tgt_width
            tgt_width = round(box_width * face_ratio)
            tgt_height = round(tgt_width * r)
        else:
            r = tgt_width / tgt_height
            tgt_height = round(box_height * face_ratio)
            tgt_width = round(tgt_height * r) 


    # upscale raw image if target size is over raw image size
    if img_width < tgt_width or img_height < tgt_height:
        if img_width < img_height:
            img_height = round(tgt_width * img_height / img_width)
            img_width = tgt_width
            img = img.resize((img_width, img_height))
        else:
            img_width = round(tgt_height * img_width / img_height)
            img_height = tgt_height
            img = img.resize((img_width, img_height)) 

    left = point[0] - tgt_width // 2
    top = point[1] - tgt_height // 2
    right = point[0] + tgt_width // 2
    bottom = point[1] + tgt_height // 2

    if left < 0:
        right -= left
        left = 0
    if right > img_width:
        left -= (right-img_width)
        right = img_width
    if top < 0:
        bottom -= top
        top = 0
    if bottom > img_height:
        top -= (bottom-img_height)
        bottom = img_height

    cropped_img = img.crop((left, top, right, bottom))
    cropped_img = cropped_img.resize(size)
    return np.ndarray(cropped_img)