import os import sys from PIL import Image # 将项目根目录添加到 Python 路径中,以便导入 poster 模块 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from poster.templates.vibrant_template import VibrantTemplate from poster.templates.business_template import BusinessTemplate def test_templates(): """ 测试 VibrantTemplate 和 BusinessTemplate 的海报生成功能。 """ # 定义资源和输出目录 image_dir = '/root/autodl-tmp/TCC_RESTRUCT/resource/data/images/天津冒险湾' output_dir = 'tests/output' os.makedirs(output_dir, exist_ok=True) # --- 1. 测试 VibrantTemplate --- print("正在测试 VibrantTemplate...") try: vibrant_config = { 'image_path': os.path.join(image_dir, '微信图片_20250703104552.jpg'), 'content': { "title": "探索天津冒险湾", "slogan": "奇幻水上乐园,家庭欢乐首选!", "price": "199", "ticket_type": "夜场票", "content_button": "套餐内容", "content_items": [ "体验所有水上滑梯", "享受家庭欢乐时光", "品尝美味海滨小吃", "参与精彩互动表演" ], "remarks": [ "工作日可直接入园", "周末请提前1天预约" ], "tag": "#夏日特惠" } } vibrant_template = VibrantTemplate() vibrant_poster = vibrant_template.generate( image_path=vibrant_config['image_path'], content=vibrant_config['content'] ) vibrant_output_path = os.path.join(output_dir, 'vibrant_poster_test.png') vibrant_poster.save(vibrant_output_path) print(f"VibrantTemplate 测试成功,海报已保存至: {vibrant_output_path}") except Exception as e: print(f"VibrantTemplate 测试失败: {e}") # --- 2. 测试 BusinessTemplate --- print("\n正在测试 BusinessTemplate...") try: # 保持与原始文件一致的 "hotel_info" 结构 hotel_info = { "name": "天津冒险湾:商业合作新机遇", "feature": "携手共创文旅新篇章", "slogan": "打造顶级水上文旅品牌", "price": "洽谈", "info_list": [ {'title': '战略合作', 'desc': '与顶级品牌联合,提升影响力。'}, {'title': '市场推广', 'desc': '覆盖全媒体渠道,精准触达目标客户。'}, {'title': '活动承办', 'desc': '承接各类大型商业活动,设施齐全。'}, {'title': '投资前景', 'desc': '高回报率的文旅投资项目,前景广阔。'} ], "footer": [ "联系我们:partner@adventurebay.com", "地址:天津市滨海新区" ] } business_config = { 'top_image_path': os.path.join(image_dir, '微信图片_20250703104600.jpg'), 'bottom_image_path': os.path.join(image_dir, '微信图片_20250703104605.jpg'), 'small_image_paths': [os.path.join(image_dir, '微信图片_20250703104609.jpg')], 'content': hotel_info # 传递完整的 "hotel_info" } business_template = BusinessTemplate() business_poster = business_template.generate(**business_config) # 将 RGBA 模式转换为 RGB 模式以便保存为 JPG if business_poster.mode == 'RGBA': business_poster = business_poster.convert('RGB') business_output_path = os.path.join(output_dir, 'business_poster_test.png') business_poster.save(business_output_path) print(f"BusinessTemplate 测试成功,海报已保存至: {business_output_path}") except Exception as e: print(f"BusinessTemplate 测试失败: {e}") if __name__ == "__main__": test_templates()