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