This commit is contained in:
HuangHai
2026-01-20 09:26:42 +08:00
parent 5e21423101
commit 79954171c6
2 changed files with 58 additions and 6 deletions

View File

@@ -75,8 +75,9 @@ async def refine_prompt(req: RefineRequest):
1. 描述画面主体、背景、光影、色彩、构图。
2. 风格要求现代感、科技感、精美、3D渲染风格或高品质插画风格。
3. 融入新能源、绿色环保、充电桩等元素。
4. 直接输出提示词内容,不要包含“好的”、“以下是”等无关废话
5. 字数在100-300字之间
4. 关于品牌元素画面中可自然融入品牌Logo的视觉元素如配色、形状能用多少就用多少有元素体现即可不必生搬硬套保持画面自然和谐
5. 直接输出提示词内容,不要包含“好的”、“以下是”等无关废话
6. 字数在100-300字之间。
"""
refined_prompt = ""
@@ -116,8 +117,9 @@ async def generate_poster(req: GenerateRequest):
1. 描述画面主体、背景、光影、色彩、构图。
2. 风格要求现代感、科技感、精美、3D渲染风格或高品质插画风格。
3. 融入新能源、绿色环保、充电桩等元素。
4. 直接输出提示词内容,不要包含“好的”、“以下是”等无关废话
5. 字数在100-300字之间
4. 关于品牌元素画面中可自然融入品牌Logo的视觉元素如配色、形状能用多少就用多少有元素体现即可不必生搬硬套保持画面自然和谐
5. 直接输出提示词内容,不要包含“好的”、“以下是”等无关废话
6. 字数在100-300字之间。
"""
refined_prompt = ""
@@ -133,7 +135,9 @@ async def generate_poster(req: GenerateRequest):
# 1.2 调用生图
resp = await client.generate_image(prompt=final_prompt, size=f"{req.width}x{req.height}")
obs_urls = await client.download_and_upload_to_obs(resp)
# 定义Logo路径
LOGO_PATH = r"d:\dsWork\aiData\static\Images\login_logo.png"
obs_urls = await client.download_and_upload_to_obs(resp, overlay_logo_path=LOGO_PATH)
if not obs_urls:
raise Exception("未获取到有效的图片URL")
return obs_urls[0]

View File

@@ -363,12 +363,13 @@ class BananaClient:
else:
logger.info("未找到有效的图片 URL")
async def download_and_upload_to_obs(self, response_json):
async def download_and_upload_to_obs(self, response_json, overlay_logo_path=None):
"""
异步从临时图片URL下载图片并直接上传到OBS不落盘
参数:
response_json: API 响应的 JSON 数据
overlay_logo_path: 水印Logo图片路径可选
返回:
uploaded_urls: 上传到OBS的图片URL列表
@@ -414,6 +415,53 @@ class BananaClient:
image_data = await response.read()
logger.debug(f"成功下载图片,大小: {len(image_data)} 字节")
# 添加水印逻辑
if overlay_logo_path and os.path.exists(overlay_logo_path):
try:
from PIL import Image
import io
logger.info(f"正在添加水印: {overlay_logo_path}")
# 打开主图
img = Image.open(io.BytesIO(image_data))
# 打开Logo
logo = Image.open(overlay_logo_path)
# 计算Logo大小 (宽度为原图的20%)
target_logo_width = int(img.width * 0.2)
aspect_ratio = logo.height / logo.width
target_logo_height = int(target_logo_width * aspect_ratio)
# 调整Logo大小
logo = logo.resize((target_logo_width, target_logo_height), Image.Resampling.LANCZOS)
# 计算位置 (左上角3% padding)
padding = int(img.width * 0.03)
position = (padding, padding)
# 粘贴Logo (处理透明度)
if logo.mode in ('RGBA', 'LA') or (logo.mode == 'P' and 'transparency' in logo.info):
img.paste(logo, position, logo)
else:
img.paste(logo, position)
# 保存回bytes
output_buffer = io.BytesIO()
# 保持原格式如果无法识别则使用PNG
save_format = img.format if img.format else 'PNG'
img.save(output_buffer, format=save_format)
image_data = output_buffer.getvalue()
logger.info("水印添加成功")
except Exception as e:
logger.error(f"添加水印失败: {e}")
# 失败后继续上传原图
# 直接上传到OBS不落盘
# 直接上传到OBS不落盘
logger.info(f"正在上传到OBS: {obs_key}")
success, result = self.obs_uploader.upload_base64_image(