279 lines
17 KiB
JSON
279 lines
17 KiB
JSON
{
|
||
"file_path": "poster/poster_generator.py",
|
||
"file_size": 3654,
|
||
"line_count": 120,
|
||
"functions": [
|
||
{
|
||
"name": "__init__",
|
||
"line_start": 32,
|
||
"line_end": 50,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
},
|
||
{
|
||
"name": "config_manager",
|
||
"type_hint": "ConfigManager"
|
||
},
|
||
{
|
||
"name": "output_manager",
|
||
"type_hint": "OutputManager"
|
||
}
|
||
],
|
||
"return_type": null,
|
||
"docstring": "初始化海报生成器\n\nArgs:\n config_manager: 配置管理器\n output_manager: 输出管理器",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def __init__(self, config_manager: ConfigManager, output_manager: OutputManager):\n \"\"\"\n 初始化海报生成器\n\n Args:\n config_manager: 配置管理器\n output_manager: 输出管理器\n \"\"\"\n self.config_manager = config_manager\n self.config = config_manager.get_config('poster', PosterConfig)\n self.output_manager = output_manager\n self.image_processor = ImageProcessor()\n \n # 注册可用的模板\n self.templates = {\n \"vibrant\": VibrantTemplate,\n \"business\": BusinessTemplate,\n \"collage\": CollageTemplate,\n }",
|
||
"code_hash": "9adc80968510375151f85be39c66893a"
|
||
},
|
||
{
|
||
"name": "generate_poster",
|
||
"line_start": 52,
|
||
"line_end": 101,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
},
|
||
{
|
||
"name": "content",
|
||
"type_hint": "Dict[str, Any]"
|
||
},
|
||
{
|
||
"name": "topic_index",
|
||
"type_hint": "Union[int, str]"
|
||
},
|
||
{
|
||
"name": "template_name",
|
||
"type_hint": "Optional[str]"
|
||
}
|
||
],
|
||
"return_type": "Optional[str]",
|
||
"docstring": "为指定内容生成海报\n\nArgs:\n content: 内容数据,包含标题、正文等\n topic_index: 主题索引,用于文件命名\n template_name: 模板名称,如果为None则根据配置选择\n\nReturns:\n 生成的海报文件路径,如果生成失败则返回None",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def generate_poster(self, content: Dict[str, Any], topic_index: Union[int, str], \n template_name: Optional[str] = None) -> Optional[str]:\n \"\"\"\n 为指定内容生成海报\n\n Args:\n content: 内容数据,包含标题、正文等\n topic_index: 主题索引,用于文件命名\n template_name: 模板名称,如果为None则根据配置选择\n\n Returns:\n 生成的海报文件路径,如果生成失败则返回None\n \"\"\"\n logger.info(f\"开始为主题 {topic_index} 生成海报...\")\n \n # 1. 选择模板\n if not template_name:\n template_name = self._select_template()\n \n if template_name not in self.templates:\n logger.error(f\"模板 '{template_name}' 不存在\")\n return None\n \n template_class = self.templates[template_name]\n template = template_class(self.config.target_size)\n \n # 2. 准备内容\n title = content.get('title', '')\n text_content = content.get('content', '')\n \n if not title or not text_content:\n logger.error(\"内容缺少标题或正文\")\n return None\n \n # 3. 生成海报\n try:\n poster_image = template.generate(title, text_content)\n \n # 4. 保存海报\n output_dir = self.output_manager.get_topic_dir(topic_index)\n file_name = f\"poster_{template_name}.png\"\n file_path = output_dir / file_name\n \n poster_image.save(file_path)\n logger.info(f\"海报已保存到: {file_path}\")\n \n return str(file_path)\n except Exception as e:\n logger.error(f\"生成海报时发生错误: {e}\", exc_info=True)\n return None",
|
||
"code_hash": "2b0daee36c09366b3a71e972294810c6"
|
||
},
|
||
{
|
||
"name": "_select_template",
|
||
"line_start": 103,
|
||
"line_end": 121,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
}
|
||
],
|
||
"return_type": "str",
|
||
"docstring": "根据配置选择模板",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def _select_template(self) -> str:\n \"\"\"根据配置选择模板\"\"\"\n selection_method = self.config.template_selection\n available_templates = self.config.available_templates\n \n # 过滤出实际可用的模板\n valid_templates = [t for t in available_templates if t in self.templates]\n \n if not valid_templates:\n logger.warning(\"没有可用的模板,使用默认模板\")\n return next(iter(self.templates.keys()))\n \n if selection_method == \"random\":\n return random.choice(valid_templates)\n elif selection_method in valid_templates:\n return selection_method\n else:\n logger.warning(f\"无效的模板选择方法 '{selection_method}',使用随机选择\")\n return random.choice(valid_templates) ",
|
||
"code_hash": "3321717ec7994b4cdcf0ac6e004298c7"
|
||
}
|
||
],
|
||
"classes": [
|
||
{
|
||
"name": "PosterGenerator",
|
||
"line_start": 26,
|
||
"line_end": 121,
|
||
"bases": [],
|
||
"methods": [
|
||
{
|
||
"name": "__init__",
|
||
"line_start": 32,
|
||
"line_end": 50,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
},
|
||
{
|
||
"name": "config_manager",
|
||
"type_hint": "ConfigManager"
|
||
},
|
||
{
|
||
"name": "output_manager",
|
||
"type_hint": "OutputManager"
|
||
}
|
||
],
|
||
"return_type": null,
|
||
"docstring": "初始化海报生成器\n\nArgs:\n config_manager: 配置管理器\n output_manager: 输出管理器",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def __init__(self, config_manager: ConfigManager, output_manager: OutputManager):\n \"\"\"\n 初始化海报生成器\n\n Args:\n config_manager: 配置管理器\n output_manager: 输出管理器\n \"\"\"\n self.config_manager = config_manager\n self.config = config_manager.get_config('poster', PosterConfig)\n self.output_manager = output_manager\n self.image_processor = ImageProcessor()\n \n # 注册可用的模板\n self.templates = {\n \"vibrant\": VibrantTemplate,\n \"business\": BusinessTemplate,\n \"collage\": CollageTemplate,\n }",
|
||
"code_hash": "9adc80968510375151f85be39c66893a"
|
||
},
|
||
{
|
||
"name": "generate_poster",
|
||
"line_start": 52,
|
||
"line_end": 101,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
},
|
||
{
|
||
"name": "content",
|
||
"type_hint": "Dict[str, Any]"
|
||
},
|
||
{
|
||
"name": "topic_index",
|
||
"type_hint": "Union[int, str]"
|
||
},
|
||
{
|
||
"name": "template_name",
|
||
"type_hint": "Optional[str]"
|
||
}
|
||
],
|
||
"return_type": "Optional[str]",
|
||
"docstring": "为指定内容生成海报\n\nArgs:\n content: 内容数据,包含标题、正文等\n topic_index: 主题索引,用于文件命名\n template_name: 模板名称,如果为None则根据配置选择\n\nReturns:\n 生成的海报文件路径,如果生成失败则返回None",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def generate_poster(self, content: Dict[str, Any], topic_index: Union[int, str], \n template_name: Optional[str] = None) -> Optional[str]:\n \"\"\"\n 为指定内容生成海报\n\n Args:\n content: 内容数据,包含标题、正文等\n topic_index: 主题索引,用于文件命名\n template_name: 模板名称,如果为None则根据配置选择\n\n Returns:\n 生成的海报文件路径,如果生成失败则返回None\n \"\"\"\n logger.info(f\"开始为主题 {topic_index} 生成海报...\")\n \n # 1. 选择模板\n if not template_name:\n template_name = self._select_template()\n \n if template_name not in self.templates:\n logger.error(f\"模板 '{template_name}' 不存在\")\n return None\n \n template_class = self.templates[template_name]\n template = template_class(self.config.target_size)\n \n # 2. 准备内容\n title = content.get('title', '')\n text_content = content.get('content', '')\n \n if not title or not text_content:\n logger.error(\"内容缺少标题或正文\")\n return None\n \n # 3. 生成海报\n try:\n poster_image = template.generate(title, text_content)\n \n # 4. 保存海报\n output_dir = self.output_manager.get_topic_dir(topic_index)\n file_name = f\"poster_{template_name}.png\"\n file_path = output_dir / file_name\n \n poster_image.save(file_path)\n logger.info(f\"海报已保存到: {file_path}\")\n \n return str(file_path)\n except Exception as e:\n logger.error(f\"生成海报时发生错误: {e}\", exc_info=True)\n return None",
|
||
"code_hash": "2b0daee36c09366b3a71e972294810c6"
|
||
},
|
||
{
|
||
"name": "_select_template",
|
||
"line_start": 103,
|
||
"line_end": 121,
|
||
"args": [
|
||
{
|
||
"name": "self"
|
||
}
|
||
],
|
||
"return_type": "str",
|
||
"docstring": "根据配置选择模板",
|
||
"is_async": false,
|
||
"decorators": [],
|
||
"code": " def _select_template(self) -> str:\n \"\"\"根据配置选择模板\"\"\"\n selection_method = self.config.template_selection\n available_templates = self.config.available_templates\n \n # 过滤出实际可用的模板\n valid_templates = [t for t in available_templates if t in self.templates]\n \n if not valid_templates:\n logger.warning(\"没有可用的模板,使用默认模板\")\n return next(iter(self.templates.keys()))\n \n if selection_method == \"random\":\n return random.choice(valid_templates)\n elif selection_method in valid_templates:\n return selection_method\n else:\n logger.warning(f\"无效的模板选择方法 '{selection_method}',使用随机选择\")\n return random.choice(valid_templates) ",
|
||
"code_hash": "3321717ec7994b4cdcf0ac6e004298c7"
|
||
}
|
||
],
|
||
"docstring": "海报生成器\n负责根据文本内容和配置生成海报图像",
|
||
"decorators": [],
|
||
"code": "class PosterGenerator:\n \"\"\"\n 海报生成器\n 负责根据文本内容和配置生成海报图像\n \"\"\"\n\n def __init__(self, config_manager: ConfigManager, output_manager: OutputManager):\n \"\"\"\n 初始化海报生成器\n\n Args:\n config_manager: 配置管理器\n output_manager: 输出管理器\n \"\"\"\n self.config_manager = config_manager\n self.config = config_manager.get_config('poster', PosterConfig)\n self.output_manager = output_manager\n self.image_processor = ImageProcessor()\n \n # 注册可用的模板\n self.templates = {\n \"vibrant\": VibrantTemplate,\n \"business\": BusinessTemplate,\n \"collage\": CollageTemplate,\n }\n\n def generate_poster(self, content: Dict[str, Any], topic_index: Union[int, str], \n template_name: Optional[str] = None) -> Optional[str]:\n \"\"\"\n 为指定内容生成海报\n\n Args:\n content: 内容数据,包含标题、正文等\n topic_index: 主题索引,用于文件命名\n template_name: 模板名称,如果为None则根据配置选择\n\n Returns:\n 生成的海报文件路径,如果生成失败则返回None\n \"\"\"\n logger.info(f\"开始为主题 {topic_index} 生成海报...\")\n \n # 1. 选择模板\n if not template_name:\n template_name = self._select_template()\n \n if template_name not in self.templates:\n logger.error(f\"模板 '{template_name}' 不存在\")\n return None\n \n template_class = self.templates[template_name]\n template = template_class(self.config.target_size)\n \n # 2. 准备内容\n title = content.get('title', '')\n text_content = content.get('content', '')\n \n if not title or not text_content:\n logger.error(\"内容缺少标题或正文\")\n return None\n \n # 3. 生成海报\n try:\n poster_image = template.generate(title, text_content)\n \n # 4. 保存海报\n output_dir = self.output_manager.get_topic_dir(topic_index)\n file_name = f\"poster_{template_name}.png\"\n file_path = output_dir / file_name\n \n poster_image.save(file_path)\n logger.info(f\"海报已保存到: {file_path}\")\n \n return str(file_path)\n except Exception as e:\n logger.error(f\"生成海报时发生错误: {e}\", exc_info=True)\n return None\n \n def _select_template(self) -> str:\n \"\"\"根据配置选择模板\"\"\"\n selection_method = self.config.template_selection\n available_templates = self.config.available_templates\n \n # 过滤出实际可用的模板\n valid_templates = [t for t in available_templates if t in self.templates]\n \n if not valid_templates:\n logger.warning(\"没有可用的模板,使用默认模板\")\n return next(iter(self.templates.keys()))\n \n if selection_method == \"random\":\n return random.choice(valid_templates)\n elif selection_method in valid_templates:\n return selection_method\n else:\n logger.warning(f\"无效的模板选择方法 '{selection_method}',使用随机选择\")\n return random.choice(valid_templates) ",
|
||
"code_hash": "aaff6457a0208a567d7dbb4767bd39d5"
|
||
}
|
||
],
|
||
"imports": [
|
||
{
|
||
"type": "import",
|
||
"modules": [
|
||
"os"
|
||
],
|
||
"aliases": []
|
||
},
|
||
{
|
||
"type": "import",
|
||
"modules": [
|
||
"logging"
|
||
],
|
||
"aliases": []
|
||
},
|
||
{
|
||
"type": "import",
|
||
"modules": [
|
||
"random"
|
||
],
|
||
"aliases": []
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "typing",
|
||
"names": [
|
||
"List",
|
||
"Dict",
|
||
"Any",
|
||
"Optional",
|
||
"Union",
|
||
"Tuple"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "pathlib",
|
||
"names": [
|
||
"Path"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "PIL",
|
||
"names": [
|
||
"Image"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "core.config",
|
||
"names": [
|
||
"ConfigManager",
|
||
"PosterConfig"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "utils.file_io",
|
||
"names": [
|
||
"OutputManager"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "templates.base_template",
|
||
"names": [
|
||
"BaseTemplate"
|
||
],
|
||
"aliases": [],
|
||
"level": 1
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "templates.vibrant_template",
|
||
"names": [
|
||
"VibrantTemplate"
|
||
],
|
||
"aliases": [],
|
||
"level": 1
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "templates.business_template",
|
||
"names": [
|
||
"BusinessTemplate"
|
||
],
|
||
"aliases": [],
|
||
"level": 1
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "templates.collage_template",
|
||
"names": [
|
||
"CollageTemplate"
|
||
],
|
||
"aliases": [],
|
||
"level": 1
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "utils",
|
||
"names": [
|
||
"ImageProcessor"
|
||
],
|
||
"aliases": [],
|
||
"level": 1
|
||
}
|
||
],
|
||
"constants": [],
|
||
"docstring": "海报生成器模块",
|
||
"content_hash": "9ff80767c00a954140776bb5a3144ec9"
|
||
} |