feat(新功能):
fix(修复bug): 模特新增后处理,保持输入图片size docs(文档变更): refactor(重构): test(增加测试):
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
|
|
||||||
from app.core.config import MINIO_URL, MINIO_ACCESS, MINIO_SECRET, MINIO_SECURE
|
from app.core.config import MINIO_URL, MINIO_ACCESS, MINIO_SECRET, MINIO_SECURE
|
||||||
@@ -34,6 +35,40 @@ class MannequinEditService():
|
|||||||
req = oss_upload_image(oss_client=minio_client, bucket=self.bucket_name, object_name=f"{self.mannequin_name}.png", image_bytes=image_bytes)
|
req = oss_upload_image(oss_client=minio_client, bucket=self.bucket_name, object_name=f"{self.mannequin_name}.png", image_bytes=image_bytes)
|
||||||
return req.bucket_name + "/" + req.object_name
|
return req.bucket_name + "/" + req.object_name
|
||||||
|
|
||||||
|
def post_processing(self, image):
|
||||||
|
# 原始图片的尺寸
|
||||||
|
original_width, original_height = image.size
|
||||||
|
|
||||||
|
# 计算宽度和高度的缩放比例
|
||||||
|
width_ratio = self.w / original_width
|
||||||
|
height_ratio = self.h / original_height
|
||||||
|
|
||||||
|
# 选择较小的缩放比例,确保图片能完整放入目标图片中
|
||||||
|
scale_ratio = min(width_ratio, height_ratio)
|
||||||
|
|
||||||
|
# 计算调整后的尺寸
|
||||||
|
new_width = int(original_width * scale_ratio)
|
||||||
|
new_height = int(original_height * scale_ratio)
|
||||||
|
|
||||||
|
# 调整图片大小
|
||||||
|
resized_image = image.resize((new_width, new_height))
|
||||||
|
|
||||||
|
# 创建一个 512x768 的透明图片
|
||||||
|
result_image = Image.new("RGBA", (self.w, self.h), (255, 255, 255, 0))
|
||||||
|
|
||||||
|
# 计算需要粘贴的位置,使图片居中
|
||||||
|
x_offset = (self.w - new_width) // 2
|
||||||
|
y_offset = (self.h - new_height) // 2
|
||||||
|
|
||||||
|
# 将调整大小后的图片粘贴到透明图片上
|
||||||
|
if resized_image.mode == "RGBA":
|
||||||
|
result_image.paste(resized_image, (x_offset, y_offset), mask=resized_image.split()[3])
|
||||||
|
else:
|
||||||
|
result_image.paste(resized_image, (x_offset, y_offset))
|
||||||
|
|
||||||
|
image = np.array(result_image)
|
||||||
|
return image
|
||||||
|
|
||||||
def resize_leg(self, top, bottom):
|
def resize_leg(self, top, bottom):
|
||||||
# 上部
|
# 上部
|
||||||
top_part = self.bgr[:top, :]
|
top_part = self.bgr[:top, :]
|
||||||
@@ -62,6 +97,7 @@ class MannequinEditService():
|
|||||||
new_image = np.dstack((new_bgr, new_bgr_alpha))
|
new_image = np.dstack((new_bgr, new_bgr_alpha))
|
||||||
else:
|
else:
|
||||||
new_image = new_bgr
|
new_image = new_bgr
|
||||||
|
new_image = self.post_processing(Image.fromarray(new_image))
|
||||||
return new_image
|
return new_image
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user