diff --git a/meme_generator/memes/directed_by/__init__.py b/meme_generator/memes/directed_by/__init__.py new file mode 100644 index 00000000..38684ed7 --- /dev/null +++ b/meme_generator/memes/directed_by/__init__.py @@ -0,0 +1,83 @@ +from datetime import datetime +from pathlib import Path + +from pil_utils import BuildImage, Text2Image +from pypinyin import lazy_pinyin + +from meme_generator import CommandShortcut, add_meme + +img_dir = Path(__file__).parent / "images" + + +def directed_by(images: list[BuildImage], texts: list[str], args): + font_families = ["Zhi Mang Xing"] + + name = texts[0][:-2] + pinyin = "".join(lazy_pinyin(name)) + pinyin = pinyin.title() + + # 准备水印文本 + line1 = f"{name}作品" + line2 = f"Directed By {pinyin}" + + # 加载基础图片 + img = images[0].convert("RGBA") + width, height = img.size + + # 计算水印区域 (高度为图片的25%) + watermark_height = int(height * 0.25) + + # 创建新画布 (带透明背景) + frame = BuildImage.new("RGBA", (width, height), (0, 0, 0, 0)) + frame.paste(img, (0, 0)) + + # 计算文字位置 (居中,宽度80%) + text_area_width = int(width * 0.8) + x = (width - text_area_width) // 2 + y = height - int(height * 0.4) + + # 绘制第一行 (中文) + text1 = Text2Image.from_text( + line1, + font_size=int(width * 0.18), # 动态字体大小 + font_families=font_families, + fill="red", + ).to_image() + + text2 = Text2Image.from_text( + line2, font_size=int(width * 0.07), fill="red" + ).to_image() + + # 计算居中位置 + text1_x = x + (text_area_width - text1.width) // 2 + text1_y = y + (watermark_height // 2 - text1.height) // 2 + + text2_x = x + (text_area_width - text2.width) // 2 + text2_y = text1_y + text1.height + + # 粘贴文字a + frame.paste(text1, (text1_x, text1_y), alpha=True) + frame.paste(text2, (text2_x, int(text2_y)), alpha=True) + + return frame.save_png() + + +add_meme( + "directed_by", + directed_by, + min_images=1, + max_images=1, + min_texts=0, + max_texts=1, + default_texts=["夏思源作品"], + keywords=["夏思源作品"], + shortcuts=[ + CommandShortcut( + key=r"(?P\S+作品[!!]?)", + args=["{text}"], + humanized="xx作品", + ) + ], + date_created=datetime(2025, 8, 6), + date_modified=datetime(2025, 8, 6), +) diff --git a/poetry.lock b/poetry.lock index c71f6ef2..3d819d0a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.1.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.1.4 and should not be changed by hand. [[package]] name = "annotated-types" @@ -879,6 +879,18 @@ files = [ [package.extras] windows-terminal = ["colorama (>=0.4.6)"] +[[package]] +name = "pypinyin" +version = "0.55.0" +description = "汉字拼音转换模块/工具." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,<4,>=2.6" +groups = ["main"] +files = [ + {file = "pypinyin-0.55.0-py2.py3-none-any.whl", hash = "sha256:d53b1e8ad2cdb815fb2cb604ed3123372f5a28c6f447571244aca36fc62a286f"}, + {file = "pypinyin-0.55.0.tar.gz", hash = "sha256:b5711b3a0c6f76e67408ec6b2e3c4987a3a806b7c528076e7c7b86fcf0eaa66b"}, +] + [[package]] name = "python-dateutil" version = "2.9.0.post0" @@ -1354,4 +1366,4 @@ dev = ["black (>=19.3b0) ; python_version >= \"3.6\"", "pytest (>=4.6.2)"] [metadata] lock-version = "2.1" python-versions = "^3.9" -content-hash = "79416bdc33b3ccb42e078badf1d31448dad07cf45b28352ffe3f45c840f1929a" +content-hash = "a4ffece4a6c51df588a9321fd347e4b06a67291950e573b6f5fb98b3d40fa52e" diff --git a/pyproject.toml b/pyproject.toml index a0951bb9..08fcb844 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ typing-extensions = ">=4.4.0,<5.0.0" arclet-alconna = "^1.8.23,!=1.8.27" arclet-alconna-tools = "^0.7.9" skia-python = ">=138.0" +pypinyin = "^0.55.0" [tool.poetry.group.dev.dependencies] diff --git a/resources/fonts/ZhiMangXing-Regular.ttf b/resources/fonts/ZhiMangXing-Regular.ttf new file mode 100644 index 00000000..d037d1f0 Binary files /dev/null and b/resources/fonts/ZhiMangXing-Regular.ttf differ