AutoML Vision在处理尺寸小于224x224像素的图像时,可以通过以下方法进行处理:
import cv2
def resize_image(image_path, new_size):
img = cv2.imread(image_path)
resized_img = cv2.resize(img, new_size)
cv2.imwrite(image_path, resized_img)
# 调整图像尺寸为224x224像素
image_path = "path_to_your_image.jpg"
new_size = (224, 224)
resize_image(image_path, new_size)
from PIL import Image
def pad_image(image_path, target_size):
img = Image.open(image_path)
old_size = img.size
# 计算填充的像素数量
delta_w = target_size[0] - old_size[0]
delta_h = target_size[1] - old_size[1]
# 计算填充的边距
padding = (delta_w // 2, delta_h // 2, delta_w - (delta_w // 2), delta_h - (delta_h // 2))
# 使用白色填充图像
padded_img = ImageOps.expand(img, padding, fill='white')
# 调整图像尺寸为目标大小
resized_img = padded_img.resize(target_size)
# 保存填充后的图像
resized_img.save(image_path)
# 调整图像尺寸为224x224像素并进行填充
image_path = "path_to_your_image.jpg"
target_size = (224, 224)
pad_image(image_path, target_size)
无论使用哪种方法,都需要确保调整后的图像能够保持其内容的完整性和清晰度。