From 3a0c730e9cfb7987431562153379c57459be2e1f Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Tue, 4 Feb 2025 10:05:15 +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:=20docs?= =?UTF-8?q?=EF=BC=88=E6=96=87=E6=A1=A3=E5=8F=98=E6=9B=B4=EF=BC=89:=20refac?= =?UTF-8?q?tor=EF=BC=88=E9=87=8D=E6=9E=84=EF=BC=89:=20test(=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B5=8B=E8=AF=95):=20=E6=97=A7=E7=89=88product=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service_generate_product_image.py | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/app/service/generate_image/service_generate_product_image.py b/app/service/generate_image/service_generate_product_image.py index 3663643..a575f07 100644 --- a/app/service/generate_image/service_generate_product_image.py +++ b/app/service/generate_image/service_generate_product_image.py @@ -331,18 +331,33 @@ def infer_cancel(tasks_id): def pre_processing_image(image_url): image = oss_get_image(bucket=image_url.split('/')[0], object_name=image_url[image_url.find('/') + 1:], data_type="PIL") - # 调整图片高度为768像素,保持宽高比 - width, height = image.size - new_height = 768 - new_width = int(width * (new_height / height)) + # 目标图片的尺寸 + target_width = 512 + target_height = 768 + + # 原始图片的尺寸 + original_width, original_height = image.size + + # 计算宽度和高度的缩放比例 + width_ratio = target_width / original_width + height_ratio = target_height / 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", (512, 768), (255, 255, 255, 255)) + # 创建一个 512x768 的透明图片 + result_image = Image.new("RGBA", (target_width, target_height), (255, 255, 255, 0)) # 计算需要粘贴的位置,使图片居中 - x_offset = (512 - new_width) // 2 - y_offset = 0 + x_offset = (target_width - new_width) // 2 + y_offset = (target_height - new_height) // 2 # 将调整大小后的图片粘贴到透明图片上 result_image.paste(resized_image, (x_offset, y_offset), mask=resized_image.split()[3]) @@ -371,8 +386,8 @@ if __name__ == '__main__': tasks_id="123-89", # prompt="", image_strength=0.7, - prompt="The best quality, masterpiece, real image.,high quality clothing details,8K realistic,HDR", - image_url="aida-results/result_40c7924e-e220-11ef-8ea2-0242ac150003.png", + prompt=" The best quality, masterpiece, real image.Outwear,high quality clothing details,8K realistic,HDR", + image_url="aida-results/result_40b1a2fe-e220-11ef-9bfa-0242ac150003.png", product_type="single" ) server = GenerateProductImage(rd)