File size: 621 Bytes
80d68c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import smartcrop
from PIL import Image


def crop_image(image, width=224, height=224):
    new_image = Image.new('RGB', image.size)
    new_image.paste(image)
    image = new_image

    sc = smartcrop.SmartCrop()

    result = sc.crop(image, width=100, height=int(width / height * 100))

    box = (
        result['top_crop']['x'],
        result['top_crop']['y'],
        result['top_crop']['width'] + result['top_crop']['x'],
        result['top_crop']['height'] + result['top_crop']['y']
    )

    cropped_image = image.crop(box)
    cropped_image.thumbnail((width, height), Image.ANTIALIAS)
    return cropped_image