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

@@ -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(