深夜提醒

现在是深夜,建议您注意休息,不要熬夜哦~

🏮 🏮 🏮

新年快乐

祝君万事如意心想事成!

2024 桐庐半程马拉松
00:00:00
时间
0.00
距离(公里)
--:--
配速
--
步频
--
心率 (bpm)
--
配速
步频
|
share-image
ESC

构建 AI 自动化文章配图系统:从 DeepSeek 到 OSS 的完整实践

作为技术博主,每次写完文章都要为找配图发愁。要么花时间找合适的图,要么干脆不用图——但后者会让文章显得单调。最近我实现了一套AI 自动化文章配图系统,写完文章后一键生成与内容匹配的封面图。这篇文章分享完整的技术实现。

构建 AI 自动化文章配图系统

需求分析

核心诉求

  1. 自动化:输入文章,自动输出配图
  2. 内容相关:图片要体现文章主题,不是随机图
  3. 批量处理:支持历史文章批量生成
  4. 自动插入:生成后自动插入到文章合适位置

技术选型考量

方案 优点 缺点
搜索网络图片 免费、快速 版权问题、不一定匹配
AI 生成(Midjourney) 质量高 需要 Discord、API 不方便
DeepSeek + 通义万相 国内可用、成本低 需要组合两个服务
DALL-E 3 效果好 需要国外信用卡

最终选择 DeepSeek + 通义万相 的组合方案:DeepSeek 负责理解文章生成提示词,通义万相负责生成图片。

系统架构

┌─────────────────┐     ┌──────────────┐     ┌─────────────┐
│ Markdown文章 │────▶│ DeepSeek │────▶│ 英文提示词 │
│ (标题+正文) │ │ (文本理解) │ │ │
└─────────────────┘ └──────────────┘ └──────┬──────┘

┌──────────────────────┘

┌─────────────────┐ ┌──────────────┐ ┌─────────────┐
│ Markdown+图片 │◀────│ 自动插入 │◀────│ 阿里云OSS │
│ (更新后的文章) │ │ (指定位置) │ │ (CDN加速) │
└─────────────────┘ └──────────────┘ └─────────────┘


┌──────┴──────┐
│ 通义万相 │
│ (图片生成) │
└─────────────┘

工作流程

  1. 解析文章提取标题和摘要
  2. DeepSeek 生成专业英文提示词
  3. 通义万相根据提示词生成图片
  4. 上传到阿里云 OSS
  5. 按策略插入到文章指定位置

核心实现

1. 文章解析模块

需要从 Markdown 中提取有效信息:

def extract_article_content(file_path):
"""提取文章标题和摘要"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

# 提取 front matter
front_matter_match = re.match(r'^---\s*\n(.*?)\n---\s*\n', content, re.DOTALL)
front_matter = front_matter_match.group(1)

# 提取标题
title_match = re.search(r'^title:\s*(.+)$', front_matter, re.MULTILINE)
title = title_match.group(1).strip() if title_match else ""

# 提取正文并清理
body_content = content[front_matter_match.end():]
# 去除代码块、图片标记等
body_content = re.sub(r'```[\s\S]*?```', '', body_content)
body_content = re.sub(r'!\[([^\]]*)\]\([^\)]+\)', '', body_content)

# 提取前 500 字作为摘要
summary = body_content.strip()[:500]

return {'title': title, 'summary': summary}

2. DeepSeek 提示词生成

关键技巧:让 DeepSeek 充当”AI 绘画提示词工程师”:

system_prompt = """你是一位专业的 AI 绘画提示词工程师。根据文章内容,生成高质量的英文 AI 绘画提示词。

要求:
1. 提示词必须是英文,适合 Stable Diffusion、Midjourney、DALL-E 等
2. 风格要现代、专业、具有视觉冲击力
3. 包含场景描述、色彩、光线、构图等元素
4. 长度控制在 80-150 个单词
5. 不要包含文字、logo、水印"""

user_prompt = f"""基于以下文章生成提示词:

标题:{title}
摘要:{summary}

请直接返回英文提示词,不需要解释。"""

调用示例

def call_deepseek_api(system_prompt, user_prompt):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}

data = {
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
"temperature": 0.7,
"max_tokens": 2000
}

response = requests.post(
'https://api.deepseek.com/v1/chat/completions',
json=data, headers=headers
)

return response.json()['choices'][0]['message']['content']

生成的提示词效果

A dynamic digital illustration of two glowing chat windows floating in a dark cyberspace, one a small widget in the corner and one a large fullscreen interface, connected by a fragile, broken data stream of light particles and binary code…

3. 通义万相图片生成

使用阿里云百炼平台的同步接口:

def generate_image_with_tongyi(prompt):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {dashscope_key}'
}

data = {
"model": "qwen-image-max", # 最新版模型
"input": {
"messages": [
{"role": "user", "content": [{"text": prompt}]}
]
},
"parameters": {
"size": "1328*1328", # 1:1 正方形
"prompt_extend": True, # 智能改写
"watermark": False, # 无水印
"negative_prompt": "低分辨率,肢体畸形,画面过饱和..."
}
}

response = requests.post(
'https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation',
json=data, headers=headers
)

# 同步返回,无需轮询
image_url = response.json()['output']['choices'][0]['message']['content'][0]['image']
return download_image(image_url)

4. 智能插入策略

不是简单地把图片放在开头,而是根据文章结构智能插入:

def find_insert_position(content, strategy="after_second_paragraph"):
"""查找图片插入位置"""

if strategy == "after_second_paragraph":
# 跳过 front matter
if content.startswith('---'):
fm_end = content.find('---', 3)
content_after_fm = content[fm_end + 3:]

# 提取段落
paragraphs = []
for line in content_after_fm.split('\n'):
if line.strip() and not line.startswith('#'):
paragraphs.append(line)
elif paragraphs:
break

# 在第二个段落后插入
if len(paragraphs) >= 2:
second_para = paragraphs[1]
pos = content.find(second_para)
return pos + len(second_para)

elif strategy == "after_first_h2":
# 在第一个 ## 标题后插入
match = re.search(r'\n##\s+.+\n', content)
if match:
return match.end()

插入效果示例

---
title: 我的文章
---

这是第一个段落,通常是引言。

这是第二个段落,内容更详细的阐述。

![我的文章](https://file.awen.me/xxx.png) <-- 插入在这里

## 问题分析

这里是正文内容...

踩坑记录

坑 1:Kimi API 401 错误

最初想用 Kimi 生成提示词,但配置了 Key 却返回 401。排查发现:

# 在服务器上测试
curl https://api.moonshot.cn/v1/models \
-H "Authorization: Bearer sk-kimi-xxx"
# 返回 {"error":{"message":"Invalid Authentication"}}

原因:OpenClaw 配置的 Kimi Key 可能有 IP 白名单限制,只能在他的机器上使用。

解决:切换到 DeepSeek,同样国产、同样好用。

坑 2:OSS 上传超时

# ossutil 命令超时 120 秒
subprocess.run(cmd, timeout=120) # 超时!

解决:改用 Python SDK(oss2),支持流式上传:

import oss2
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
bucket.put_object_from_file(oss_key, local_file)

坑 3:图片 404

生成后图片 URL 返回 404,检查发现:

# 错误:文件上传到 OSS 后没有立即生效
# 正确:等待上传完成后再验证
bucket.put_object_from_file(oss_key, local_file)
assert bucket.object_exists(oss_key) # 验证存在性

使用方式

命令行使用

# 为最新文章生成封面图
npm run gen-cover:latest

# 指定文章
python3 tools/auto_insert_cover.py source/_posts/2026/03/文章.md --model deepseek

# 指定插入位置
python3 tools/auto_insert_cover.py source/_posts/2026/03/文章.md \
--strategy after_first_h2

Git 自动集成

配置了 Git pre-commit 钩子,提交文章时自动检查:

git add source/_posts/2026/03/我的文章.md
git commit -m "post: add new article"
# 自动触发:检查封面图 → 生成 → 插入 → 添加到提交

成本分析

服务 费用 说明
DeepSeek ¥0.001/千 tokens 一篇文章约 ¥0.01
通义万相 ¥0.12/张 生成一张图
OSS 存储 ¥0.12/GB/月 可忽略
总计 约 ¥0.13/篇 比一杯奶茶便宜多了

最终效果

现在写文章的工作流:

  1. hexo new "文章标题" 创建文章
  2. 写完内容
  3. npm run gen-cover:latest 一键生成配图
  4. git commit && git push 自动部署

配图与文章内容强相关,比如这篇文章的配图:

AI 生成的封面图

完美体现了”聊天窗口同步”的主题。

代码仓库

完整代码已开源:

  • tools/auto_insert_cover.py - 主脚本
  • tools/generate_image_prompt.py - 提示词生成
  • tools/generate_article_image.py - 图片生成与上传

配置好自己的 DeepSeek 和阿里云 Key 后即可使用。


总结:通过 DeepSeek + 通义万相的组合,实现了低成本、高质量的 AI 自动化配图。整个过程从文章解析到图片插入全自动完成,大大提升了写作效率。如果你也是 Hexo 用户,可以直接拿这套方案使用。

文章作者:阿文
文章链接: https://www.awen.me/post/75409bd7.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客

评论

0 条评论
😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘 🛑📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭 ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️ 🈯💹❇️ ✳️🌐 💠Ⓜ️🌀 💤🏧🚾 🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️
您的评论由 AI 智能审核,一般1分钟内会展示,若不展示请确认你的评论是否符合社区和法律规范
加载中...

选择联系方式

留言反馈

😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘 🛑📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭 ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️ 🈯💹❇️ ✳️🌐 💠Ⓜ️🌀 💤🏧🚾 🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️