fix  修复product img 头部缺失问题
This commit is contained in:
zhouchengrong
2024-07-04 15:22:40 +08:00
parent eede159507
commit 1d29d15a7c

View File

@@ -136,24 +136,39 @@ def infer_cancel(tasks_id):
def pre_processing_image(image_url): 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") image = oss_get_image(bucket=image_url.split('/')[0], object_name=image_url[image_url.find('/') + 1:], data_type="PIL")
# 原始图片的尺寸
width, height = image.size
# resize 图片内尺寸 并贴到768-512的纯白图像上 # 计算长宽比为 3:2 的新尺寸
target_height = 768 desired_ratio = 2 / 3
target_width = 512 current_ratio = width / height
aspect_ratio = image.width / image.height
new_width = int(target_height * aspect_ratio) if current_ratio > desired_ratio:
resized_image = image.resize((new_width, target_height)) # 原始图片更宽,需要在上下添加 padding
left = (target_width - resized_image.width) // 2 new_width = width
top = (target_height - resized_image.height) // 2 new_height = int(width / desired_ratio)
right = target_width - resized_image.width - left else:
bottom = target_height - resized_image.height - top # 原始图片更高或者长宽比已经为 3:2
image = ImageOps.expand(resized_image, (left, top, right, bottom), fill="white") new_height = height
image_size = image.size new_width = int(height * desired_ratio)
if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info):
# 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景
pad_image = Image.new('RGBA', (new_width, new_height), (0, 0, 0, 0))
# 将原始图片粘贴到新的画布中心
left = (new_width - width) // 2
top = (new_height - height) // 2
pad_image.paste(image, (left, top))
# 将画布 resize 成宽度 500长度 750
resized_image = pad_image.resize((500, 750))
image_size = (512, 768)
if resized_image.mode in ('RGBA', 'LA') or (resized_image.mode == 'P' and 'transparency' in resized_image.info):
# 创建白色背景 # 创建白色背景
background = Image.new("RGB", image.size, (255, 255, 255)) background = Image.new("RGB", image_size, (255, 255, 255))
# 将图片粘贴到白色背景上 # 将图片粘贴到白色背景上
background.paste(image, mask=image.split()[3]) background.paste(resized_image, mask=resized_image.split()[3])
image = np.array(background) image = np.array(background)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image, image_size return image, image_size
@@ -162,11 +177,11 @@ def pre_processing_image(image_url):
if __name__ == '__main__': if __name__ == '__main__':
rd = GenerateProductImageModel( rd = GenerateProductImageModel(
tasks_id="123-89", tasks_id="123-89",
prompt="", # prompt="",
image_strength=0.9, image_strength=0.9,
# prompt=" the best quality, masterpiece. detailed, high-res, simple background, studio photography, extremely detailed, updo, detailed face, face, close-up, HDR, UHD, 8K realistic, Highly detailed, simple background, Studio lighting", prompt=" the best quality, masterpiece. detailed, high-res, simple background, studio photography, extremely detailed, updo, detailed face, face, close-up, HDR, UHD, 8K realistic, Highly detailed, simple background, Studio lighting",
image_url="aida-results/result_00097282-ebb2-11ee-a822-b48351119060.png", image_url="aida-results/result_00097282-ebb2-11ee-a822-b48351119060.png",
product_type="single" product_type="overall"
) )
server = GenerateProductImage(rd) server = GenerateProductImage(rd)
print(server.get_result()) print(server.get_result())