From ae38a3a357e107abdd5137184017dd291df9aaac Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Tue, 1 Apr 2025 14:14:50 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=88=E6=96=B0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=89:=20fix=EF=BC=88=E4=BF=AE=E5=A4=8Dbug=EF=BC=89:=20=20?= =?UTF-8?q?=E6=A8=A1=E7=89=B9=E6=96=B0=E5=A2=9E=E5=90=8E=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E4=BF=9D=E6=8C=81=E8=BE=93=E5=85=A5=E5=9B=BE=E7=89=87?= =?UTF-8?q?size=20docs=EF=BC=88=E6=96=87=E6=A1=A3=E5=8F=98=E6=9B=B4?= =?UTF-8?q?=EF=BC=89:=20refactor=EF=BC=88=E9=87=8D=E6=9E=84=EF=BC=89:=20te?= =?UTF-8?q?st(=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/mannequins_edit/service.py | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/service/mannequins_edit/service.py b/app/service/mannequins_edit/service.py index 8db25d5..c0f0a44 100644 --- a/app/service/mannequins_edit/service.py +++ b/app/service/mannequins_edit/service.py @@ -1,5 +1,6 @@ import cv2 import numpy as np +from PIL import Image from minio import Minio 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) 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): # 上部 top_part = self.bgr[:top, :] @@ -62,6 +97,7 @@ class MannequinEditService(): new_image = np.dstack((new_bgr, new_bgr_alpha)) else: new_image = new_bgr + new_image = self.post_processing(Image.fromarray(new_image)) return new_image