大家好,我是Ai學習的老章
大模型生成知識卡片,我之前介紹過多次
比如我最近在用的banner
比如閱讀理解
還有很多很多。。。
這些都是我用大模型直接生成的,具體說,是Claude-3.7
前文詳細介紹過具體方法
今天再給大家分享一個我剛剛測試好的批量生成語錄類知識卡片的方法
其實更多的是提供思路,整個過程很多細節可以再琢磨一下。
1、生成模版.svg
模板中的文字待填充
事實上,我生成了6個模板
默認粉紫漸變風格(default) - 柔和的粉紫色漸變背景,優雅簡約
極簡禪風(minimalist_zen) - 極簡主義設計,注重留白和平衡
現代幾何風格(modern_geometric) - 深色背景配合幾何元素,現代感強
自然靈感(nature_inspired) - 自然色調和有機元素,清新自然
古典優雅(classic_elegant) - 復古紙張質感,傳統典雅
科技數字風(tech_digital) - 深色背景配合科技元素,未來感

2、生成語錄.json
這一步也是交給大模型,生成100條就對應100張卡片
3、批量生成腳本.py
其實就是把json中每條中英文語錄、作者塞進svg模板
#quote_card_generator.py import json import re import os from pathlib import Path import argparse # 可用的模板列表 TEMPLATE_OPTIONS = { "default": "默認粉紫漸變風格", "minimalist_zen": "極簡禪風", "modern_geometric": "現代幾何風格", "nature_inspired": "自然靈感", "classic_elegant": "古典優雅", "tech_digital": "科技數字風" } def process_quote(text): """處理輸入的文本,轉換為 JSON 格式""" # 預期格式:中文名言 | English quote - 作者名 | Author name pattern = r"(.*?)\s*\|\s*(.*?)\s*-\s*(.*?)\s*\|\s*(.*)" match = re.match(pattern, text) ifnot match: raise ValueError("輸入格式不正確。請使用格式:中文名言 | English quote - 作者名 | Author name") quote_cn, quote_en, author_cn, author_en = match.groups() return { "quote": { "cn": quote_cn.strip(), "en": quote_en.strip() }, "author": { "cn": author_cn.strip(), "en": author_en.strip() } } def list_templates(): """列出所有可用的模板""" print("可用的引言卡片模板:") print("-" * 40) for key, desc in TEMPLATE_OPTIONS.items(): print(f"- {key}: {desc}") print("-" * 40) def generate_svg(data, template_name="default"): """根據 JSON 數據和模板名稱生成 SVG""" # 模板目錄 templates_dir = Path(__file__).parent / "templates" # 確保模板存在,否則使用默認模板 template_path = templates_dir / f"{template_name}.svg" ifnot template_path.exists(): print(f"警告:模板 '{template_name}' 不存在,將使用默認模板。") template_path = templates_dir / "default.svg" # 讀取 SVG 模板 with open(template_path, "r", encoding="utf-8") as f: svg_template = f.read() # 根據文本長度動態調整字體大小 # 中文引言字體大小 - 較長的引言會使用較小的字體 quote_cn_size = min(60, 1000 / (len(data["quote"]["cn"]) / 2 + 5)) # 英文引言字體大小 quote_en_size = min(40, 1000 / (len(data["quote"]["en"]) / 2 + 5)) # 替換模板中的占位符 svg = svg_template.replace("{{QUOTE_CN}}", data["quote"]["cn"]) svg = svg.replace("{{QUOTE_EN}}", data["quote"]["en"]) svg = svg.replace("{{AUTHOR_CN}}", data["author"]["cn"]) svg = svg.replace("{{AUTHOR_EN}}", data["author"]["en"]) svg = svg.replace("{{QUOTE_CN_SIZE}}", str(quote_cn_size)) svg = svg.replace("{{QUOTE_EN_SIZE}}", str(quote_en_size)) return svg def main(): parser = argparse.ArgumentParser(description="生成優雅的名人名言卡片") parser.add_argument("quote", nargs="?", help="格式:中文名言 | English quote - 作者名 | Author name") parser.add_argument("--template", "-t", default="default", help="卡片模板名稱") parser.add_argument("--output", "-o", default="quote_card.svg", help="輸出文件名") parser.add_argument("--list", "-l", action="store_true", help="列出所有可用的模板") args = parser.parse_args() # 如果使用了 --list 參數,列出所有模板并退出 if args.list: list_templates() return # 如果沒有提供引言,提示用戶輸入必要參數并退出 ifnot args.quote: parser.print_help() print("\n請提供引言參數,或使用 --list 查看可用模板。") return try: # 處理輸入文本 data = process_quote(args.quote) # 生成 SVG svg_content = generate_svg(data, args.template) # 保存 SVG 文件 with open(args.output, "w", encoding="utf-8") as f: f.write(svg_content) print(f"成功生成 SVG 文件:{args.output}") print(f"使用模板:{args.template}") print("JSON 格式的數據:") print(json.dumps(data, ensure_ascii=False, indent=2)) except Exception as e: print(f"錯誤:{str(e)}") if __name__ == "__main__": main()
使用方法:python quote_card_generator.py --template minimalist_zen --limit 5
不指定template就是用default模板,不指定limit就是全部生成
4、svg轉為png
不贅述,前文講過
方便起見,你可以把文章的腳本修改成文件夾批量執行,也可以直接在命令行中批量跑
for file in /Users/zhangbeihai/Desktop/cards/generated_cards/*.svg; do /Users/zhangbeihai/Desktop/svg2png.sh "$file" done
制作不易,如果這篇文章覺得對你有用,可否點個關注。給我個三連擊:點贊、轉發和在看。若可以再給我加個,謝謝你看我的文章,我們下篇再見!
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.