File size: 3,604 Bytes
96bea52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from PIL import Image
import PIL
import cv2, math,os
import numpy as np

# 最短边为1024,并进行中心裁剪
def resize_image_pil(input_image, min_side=1024):

    # 获取图像的宽度和高度
    width, height = input_image.size
    
    # 计算缩放比例
    scale = min_side / min(height, width)
    
    # 计算新的尺寸
    new_width = int(width * scale)
    new_height = int(height * scale)
    
    # 调整图像大小
    resized_image = input_image.resize((new_width, new_height), Image.ANTIALIAS)
    
    # 计算中心裁剪的位置
    crop_top = (new_height - min_side) // 2
    crop_left = (new_width - min_side) // 2
    
    # 进行中心裁剪
    cropped_image = resized_image.crop((crop_left, crop_top, crop_left + min_side, crop_top + min_side))
    
    return cropped_image

def resize_image_cv2(input_image, min_side=1024, ):
    # cv2读取的image
    (height, width, _ )= input_image.shape
    # print(height, width)
    scale = min_side / min(height, width)
            
    # 计算新的尺寸
    new_width = int(width * scale)
    new_height = int(height * scale)
    input_image =cv2.resize(input_image, (new_width, new_height))

    # 计算中心裁剪的位置
    crop_top = (new_height - min_side) // 2
    crop_left = (new_width - min_side) // 2

    # 进行中心裁剪
    image = input_image[crop_top:crop_top + min_side, crop_left:crop_left + min_side]

    return image


def resize_img0(input_image, max_side=1280, min_side=1024, 
               mode=Image.BILINEAR, base_pixel_number=64):

    w, h = input_image.size

    ratio = min_side / min(h, w)
    w, h = round(ratio*w), round(ratio*h)
    ratio = max_side / max(h, w)
    input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
    w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
    h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number

    input_image = input_image.resize([w_resize_new, h_resize_new], mode)


    return input_image

def resize_img1(input_image, max_side=1280, min_side=1024, 
               mode=Image.BILINEAR, base_pixel_number=64):

    w, h = input_image.size

    ratio = min_side / w
    w, h = round(ratio*w), round(ratio*h)
    input_image = input_image.resize([w, h], mode)

    w_resize_new = (w // base_pixel_number) * base_pixel_number
    h_resize_new = (h // base_pixel_number) * base_pixel_number

    input_image = input_image.resize([w_resize_new, h_resize_new], mode)


    return input_image


def resize_img(input_image, max_side=1024, min_side=1024, size=None,
               pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
    w, h = input_image.size
    if size is not None:
        w_resize_new, h_resize_new = size
    else:
        ratio = min_side / min(h, w)
        w, h = round(ratio * w), round(ratio * h)
        ratio = max_side / max(h, w)
        input_image = input_image.resize([round(ratio * w), round(ratio * h)], mode)
        w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
        h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
    input_image = input_image.resize([w_resize_new, h_resize_new], mode)

    if pad_to_max_side:
        res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
        offset_x = (max_side - w_resize_new) // 2
        offset_y = (max_side - h_resize_new) // 2
        res[offset_y:offset_y + h_resize_new, offset_x:offset_x + w_resize_new] = np.array(input_image)[:, :, :3]
        input_image = Image.fromarray(res)
    return input_image