2025-07-31 15:35:23 +08:00

298 lines
15 KiB
JSON

{
"file_path": "poster/templates/base_template.py",
"file_size": 2679,
"line_count": 79,
"functions": [
{
"name": "__init__",
"line_start": 25,
"line_end": 30,
"args": [
{
"name": "self"
},
{
"name": "size",
"type_hint": "Tuple[int, int]"
},
{
"name": "font_dir",
"type_hint": "str"
}
],
"return_type": null,
"docstring": "",
"is_async": false,
"decorators": [],
"code": " def __init__(self, size: Tuple[int, int] = (900, 1200), font_dir: str = \"/root/autodl-tmp/TCC_RESTRUCT/assets/font\"):\n self.width, self.height = size\n self.size = size\n self.image_processor = ImageProcessor()\n self.text_renderer = TextRenderer(font_dir=font_dir)\n self.logger = logging.getLogger(self.__class__.__name__)",
"code_hash": "b27541bbdca01475faa41fe5b0ac574e"
},
{
"name": "generate",
"line_start": 33,
"line_end": 39,
"args": [
{
"name": "self"
}
],
"return_type": "Image.Image",
"docstring": "生成海报的核心方法。\n每个子类必须实现此方法。\nkwargs: 包含生成海报所需的所有动态参数,如图片路径、文案内容等。",
"is_async": false,
"decorators": [
"abstractmethod"
],
"code": " def generate(self, **kwargs) -> Image.Image:\n \"\"\"\n 生成海报的核心方法。\n 每个子类必须实现此方法。\n kwargs: 包含生成海报所需的所有动态参数,如图片路径、文案内容等。\n \"\"\"\n pass",
"code_hash": "bdedc295b85d8a2b1693220b23495970"
},
{
"name": "create_canvas",
"line_start": 41,
"line_end": 44,
"args": [
{
"name": "self"
},
{
"name": "background_color",
"type_hint": "Optional[Tuple[int, int, int, int]]"
}
],
"return_type": "Image.Image",
"docstring": "创建一个指定尺寸和颜色的空白画布",
"is_async": false,
"decorators": [],
"code": " def create_canvas(self, background_color: Optional[Tuple[int, int, int, int]] = None) -> Image.Image:\n \"\"\"创建一个指定尺寸和颜色的空白画布\"\"\"\n color = background_color or (255, 255, 255, 255)\n return Image.new(\"RGBA\", self.size, color)",
"code_hash": "40193d21b20dde58e8da1971ae8718e0"
},
{
"name": "create_gradient_background",
"line_start": 46,
"line_end": 72,
"args": [
{
"name": "self"
},
{
"name": "top_color",
"type_hint": "Tuple[int, int, int]"
},
{
"name": "bottom_color",
"type_hint": "Tuple[int, int, int]"
},
{
"name": "direction",
"type_hint": "str"
}
],
"return_type": "Image.Image",
"docstring": "创建渐变背景图",
"is_async": false,
"decorators": [],
"code": " def create_gradient_background(self, \n top_color: Tuple[int, int, int],\n bottom_color: Tuple[int, int, int],\n direction: str = \"vertical\") -> Image.Image:\n \"\"\"创建渐变背景图\"\"\"\n canvas = Image.new(\"RGBA\", self.size)\n draw = ImageDraw.Draw(canvas)\n \n r1, g1, b1 = top_color\n r2, g2, b2 = bottom_color\n \n if direction == \"vertical\":\n for i in range(self.height):\n ratio = i / self.height\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(0, i), (self.width, i)], fill=(r, g, b, 255))\n else: # horizontal\n for i in range(self.width):\n ratio = i / self.width\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(i, 0), (i, self.height)], fill=(r, g, b, 255))\n \n return canvas",
"code_hash": "306d5c8d97318043ae6b63f6254cf1ec"
},
{
"name": "_validate_inputs",
"line_start": 74,
"line_end": 80,
"args": [
{
"name": "self"
},
{
"name": "required_keys",
"type_hint": "List[str]"
}
],
"return_type": "bool",
"docstring": "一个简单的输入验证辅助方法",
"is_async": false,
"decorators": [],
"code": " def _validate_inputs(self, required_keys: List[str], **kwargs) -> bool:\n \"\"\"一个简单的输入验证辅助方法\"\"\"\n for key in required_keys:\n if key not in kwargs or kwargs[key] is None:\n self.logger.error(f\"生成海报失败: 缺少必需的参数 '{key}'\")\n return False\n return True ",
"code_hash": "8a9e140a9853c925a98577727ab81aad"
}
],
"classes": [
{
"name": "BaseTemplate",
"line_start": 19,
"line_end": 80,
"bases": [
"ABC"
],
"methods": [
{
"name": "__init__",
"line_start": 25,
"line_end": 30,
"args": [
{
"name": "self"
},
{
"name": "size",
"type_hint": "Tuple[int, int]"
},
{
"name": "font_dir",
"type_hint": "str"
}
],
"return_type": null,
"docstring": "",
"is_async": false,
"decorators": [],
"code": " def __init__(self, size: Tuple[int, int] = (900, 1200), font_dir: str = \"/root/autodl-tmp/TCC_RESTRUCT/assets/font\"):\n self.width, self.height = size\n self.size = size\n self.image_processor = ImageProcessor()\n self.text_renderer = TextRenderer(font_dir=font_dir)\n self.logger = logging.getLogger(self.__class__.__name__)",
"code_hash": "b27541bbdca01475faa41fe5b0ac574e"
},
{
"name": "generate",
"line_start": 33,
"line_end": 39,
"args": [
{
"name": "self"
}
],
"return_type": "Image.Image",
"docstring": "生成海报的核心方法。\n每个子类必须实现此方法。\nkwargs: 包含生成海报所需的所有动态参数,如图片路径、文案内容等。",
"is_async": false,
"decorators": [
"abstractmethod"
],
"code": " def generate(self, **kwargs) -> Image.Image:\n \"\"\"\n 生成海报的核心方法。\n 每个子类必须实现此方法。\n kwargs: 包含生成海报所需的所有动态参数,如图片路径、文案内容等。\n \"\"\"\n pass",
"code_hash": "bdedc295b85d8a2b1693220b23495970"
},
{
"name": "create_canvas",
"line_start": 41,
"line_end": 44,
"args": [
{
"name": "self"
},
{
"name": "background_color",
"type_hint": "Optional[Tuple[int, int, int, int]]"
}
],
"return_type": "Image.Image",
"docstring": "创建一个指定尺寸和颜色的空白画布",
"is_async": false,
"decorators": [],
"code": " def create_canvas(self, background_color: Optional[Tuple[int, int, int, int]] = None) -> Image.Image:\n \"\"\"创建一个指定尺寸和颜色的空白画布\"\"\"\n color = background_color or (255, 255, 255, 255)\n return Image.new(\"RGBA\", self.size, color)",
"code_hash": "40193d21b20dde58e8da1971ae8718e0"
},
{
"name": "create_gradient_background",
"line_start": 46,
"line_end": 72,
"args": [
{
"name": "self"
},
{
"name": "top_color",
"type_hint": "Tuple[int, int, int]"
},
{
"name": "bottom_color",
"type_hint": "Tuple[int, int, int]"
},
{
"name": "direction",
"type_hint": "str"
}
],
"return_type": "Image.Image",
"docstring": "创建渐变背景图",
"is_async": false,
"decorators": [],
"code": " def create_gradient_background(self, \n top_color: Tuple[int, int, int],\n bottom_color: Tuple[int, int, int],\n direction: str = \"vertical\") -> Image.Image:\n \"\"\"创建渐变背景图\"\"\"\n canvas = Image.new(\"RGBA\", self.size)\n draw = ImageDraw.Draw(canvas)\n \n r1, g1, b1 = top_color\n r2, g2, b2 = bottom_color\n \n if direction == \"vertical\":\n for i in range(self.height):\n ratio = i / self.height\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(0, i), (self.width, i)], fill=(r, g, b, 255))\n else: # horizontal\n for i in range(self.width):\n ratio = i / self.width\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(i, 0), (i, self.height)], fill=(r, g, b, 255))\n \n return canvas",
"code_hash": "306d5c8d97318043ae6b63f6254cf1ec"
},
{
"name": "_validate_inputs",
"line_start": 74,
"line_end": 80,
"args": [
{
"name": "self"
},
{
"name": "required_keys",
"type_hint": "List[str]"
}
],
"return_type": "bool",
"docstring": "一个简单的输入验证辅助方法",
"is_async": false,
"decorators": [],
"code": " def _validate_inputs(self, required_keys: List[str], **kwargs) -> bool:\n \"\"\"一个简单的输入验证辅助方法\"\"\"\n for key in required_keys:\n if key not in kwargs or kwargs[key] is None:\n self.logger.error(f\"生成海报失败: 缺少必需的参数 '{key}'\")\n return False\n return True ",
"code_hash": "8a9e140a9853c925a98577727ab81aad"
}
],
"docstring": "所有海报模板的抽象基类。\n定义了模板的通用接口和基础功能。",
"decorators": [],
"code": "class BaseTemplate(ABC):\n \"\"\"\n 所有海报模板的抽象基类。\n 定义了模板的通用接口和基础功能。\n \"\"\"\n \n def __init__(self, size: Tuple[int, int] = (900, 1200), font_dir: str = \"/root/autodl-tmp/TCC_RESTRUCT/assets/font\"):\n self.width, self.height = size\n self.size = size\n self.image_processor = ImageProcessor()\n self.text_renderer = TextRenderer(font_dir=font_dir)\n self.logger = logging.getLogger(self.__class__.__name__)\n\n @abstractmethod\n def generate(self, **kwargs) -> Image.Image:\n \"\"\"\n 生成海报的核心方法。\n 每个子类必须实现此方法。\n kwargs: 包含生成海报所需的所有动态参数,如图片路径、文案内容等。\n \"\"\"\n pass\n\n def create_canvas(self, background_color: Optional[Tuple[int, int, int, int]] = None) -> Image.Image:\n \"\"\"创建一个指定尺寸和颜色的空白画布\"\"\"\n color = background_color or (255, 255, 255, 255)\n return Image.new(\"RGBA\", self.size, color)\n\n def create_gradient_background(self, \n top_color: Tuple[int, int, int],\n bottom_color: Tuple[int, int, int],\n direction: str = \"vertical\") -> Image.Image:\n \"\"\"创建渐变背景图\"\"\"\n canvas = Image.new(\"RGBA\", self.size)\n draw = ImageDraw.Draw(canvas)\n \n r1, g1, b1 = top_color\n r2, g2, b2 = bottom_color\n \n if direction == \"vertical\":\n for i in range(self.height):\n ratio = i / self.height\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(0, i), (self.width, i)], fill=(r, g, b, 255))\n else: # horizontal\n for i in range(self.width):\n ratio = i / self.width\n r = int(r1 * (1 - ratio) + r2 * ratio)\n g = int(g1 * (1 - ratio) + g2 * ratio)\n b = int(b1 * (1 - ratio) + b2 * ratio)\n draw.line([(i, 0), (i, self.height)], fill=(r, g, b, 255))\n \n return canvas\n\n def _validate_inputs(self, required_keys: List[str], **kwargs) -> bool:\n \"\"\"一个简单的输入验证辅助方法\"\"\"\n for key in required_keys:\n if key not in kwargs or kwargs[key] is None:\n self.logger.error(f\"生成海报失败: 缺少必需的参数 '{key}'\")\n return False\n return True ",
"code_hash": "833bde30baa1943c551c83e621a86985"
}
],
"imports": [
{
"type": "import",
"modules": [
"logging"
],
"aliases": []
},
{
"type": "from_import",
"module": "abc",
"names": [
"ABC",
"abstractmethod"
],
"aliases": [],
"level": 0
},
{
"type": "from_import",
"module": "typing",
"names": [
"Tuple",
"Dict",
"Any",
"Optional",
"List"
],
"aliases": [],
"level": 0
},
{
"type": "from_import",
"module": "PIL",
"names": [
"Image",
"ImageDraw"
],
"aliases": [],
"level": 0
},
{
"type": "from_import",
"module": "utils",
"names": [
"ImageProcessor",
"TextRenderer"
],
"aliases": [],
"level": 2
}
],
"constants": [],
"docstring": "海报模板的抽象基类",
"content_hash": "45a1688c42ab638c7c7ade223f08d48b"
}