newspaper_infographic.py
9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""newspaper_infographic.py - 复古报纸风格信息图生成器 v2
Features:
- 标题区域:群名 + 日期 + 版次
- 分板块:按行业分类(AI Agent、大模型、AI硬件、行业动态等)
- 每条新闻:标题 + 简短描述
- 配图:用色块和图标代替真实图片
Usage:
python3 newspaper_infographic.py --report report.md --output infographic.png
"""
import argparse
import os
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
# 颜色方案(复古报纸)
PAPER_BG = "#f5f0e1" # 米黄色纸张
PAPER_DARK = "#e8dcc8" # 深色纸张
TEXT_BLACK = "#1a1a1a" # 黑色文字
TEXT_RED = "#8b0000" # 深红色(标题强调)
TEXT_BLUE = "#1a3a5c" # 深蓝色(板块标题)
BORDER_COLOR = "#2c2c2c" # 边框色
ACCENT_COLOR = "#c9a84c" # 金色强调
# 板块配色
CATEGORY_COLORS = {
"AI Agent": "#e8d5b7",
"大模型": "#d4e5d0",
"AI硬件": "#d0d8e8",
"行业动态": "#e8d0d0",
"开源项目": "#d0e8e0",
"其他": "#e0d0e8"
}
def load_fonts():
"""加载字体,返回不同大小的字体对象"""
font_paths = [
"/data/data/com.termux/files/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
"/system/fonts/NotoSansCJK-Regular.ttc",
"/data/data/com.termux/files/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
]
font_regular = None
for path in font_paths:
if os.path.exists(path):
try:
font_regular = ImageFont.truetype(path, 14)
font_bold = ImageFont.truetype(path, 16)
font_title = ImageFont.truetype(path, 28)
font_headline = ImageFont.truetype(path, 20)
font_small = ImageFont.truetype(path, 11)
font_large = ImageFont.truetype(path, 36)
return font_regular, font_bold, font_title, font_headline, font_small, font_large
except:
continue
# 回退到默认字体
default = ImageFont.load_default()
return default, default, default, default, default, default
def draw_text_wrapped(draw, text, x, y, max_width, font, fill, line_height=20):
"""绘制自动换行的文本,返回绘制后的底部 y 坐标"""
words = text
lines = []
current_line = ""
for char in words:
test_line = current_line + char
bbox = draw.textbbox((0, 0), test_line, font=font)
if bbox[2] - bbox[0] > max_width and current_line:
lines.append(current_line)
current_line = char
else:
current_line = test_line
if current_line:
lines.append(current_line)
for i, line in enumerate(lines):
draw.text((x, y + i * line_height), line, fill=fill, font=font)
return y + len(lines) * line_height
def parse_report(report_text):
"""解析报告内容,提取结构化数据"""
sections = []
current_section = None
current_articles = []
lines = report_text.split('\n')
i = 0
while i < len(lines):
line = lines[i].strip()
# 检测板块标题(### 开头)
if line.startswith('### '):
if current_section and current_articles:
sections.append({
'category': current_section,
'articles': current_articles
})
current_section = line[4:]
current_articles = []
i += 1
continue
# 检测文章标题(- ** 开头)
if line.startswith('- **') or line.startswith('1. **') or line.startswith('2. **'):
# 提取标题
title = line.split('**')[1] if '**' in line else line
desc = ""
# 尝试读取下一行作为描述
if i + 1 < len(lines):
next_line = lines[i + 1].strip()
if next_line.startswith(' ') or next_line.startswith('\t'):
desc = next_line.strip()
i += 1
current_articles.append({
'title': title,
'desc': desc
})
i += 1
# 添加最后一个板块
if current_section and current_articles:
sections.append({
'category': current_section,
'articles': current_articles
})
return sections
def create_newspaper_infographic(report_text, output_path, group_name="8-TG0 AI AGI"):
"""创建复古报纸风格信息图"""
WIDTH = 900
MARGIN = 30
# 加载字体
font_regular, font_bold, font_title, font_headline, font_small, font_large = load_fonts()
# 解析报告
sections = parse_report(report_text)
# 计算总高度
total_height = 200 # 标题区域
total_height += 60 # 统计栏
for section in sections:
total_height += 50 # 板块标题
for article in section['articles']:
total_height += 25 # 文章标题
if article['desc']:
total_height += 35 # 描述(换行后)
total_height += 10 # 间距
total_height += 20 # 板块间距
total_height += 60 # 页脚
total_height = max(total_height, 1200) # 最小高度
# 创建画布
img = Image.new('RGB', (WIDTH, total_height), PAPER_BG)
draw = ImageDraw.Draw(img)
# === 报纸边框 ===
draw.rectangle([3, 3, WIDTH-3, total_height-3], outline=BORDER_COLOR, width=3)
draw.rectangle([8, 8, WIDTH-8, total_height-8], outline=BORDER_COLOR, width=1)
y = 20
# === 报头 ===
# 报纸名称
draw.text((WIDTH//2, y), "AI AGI 日报", fill=TEXT_BLACK, font=font_large, anchor="mm")
y += 50
# 装饰线
draw.line([(MARGIN, y), (WIDTH-MARGIN, y)], fill=BORDER_COLOR, width=2)
y += 12
# 日期、群名、版次
date_str = datetime.now().strftime("%Y年%m月%d日")
edition_str = "第1版"
draw.text((MARGIN, y), date_str, fill=TEXT_BLACK, font=font_regular)
draw.text((WIDTH//2, y), f"{group_name}", fill=TEXT_RED, font=font_bold, anchor="mm")
draw.text((WIDTH-MARGIN, y), edition_str, fill=TEXT_BLACK, font=font_regular, anchor="ra")
y += 28
# 装饰线
draw.line([(MARGIN, y), (WIDTH-MARGIN, y)], fill=BORDER_COLOR, width=1)
y += 20
# === 统计栏 ===
total_articles = sum(len(s['articles']) for s in sections)
stats_text = f"今日精选:{total_articles} 篇文章 | {len(sections)} 个板块"
# 统计框背景
draw.rectangle([MARGIN, y, WIDTH-MARGIN, y+45], fill=PAPER_DARK, outline=BORDER_COLOR)
draw.text((WIDTH//2, y+22), stats_text, fill=TEXT_BLACK, font=font_bold, anchor="mm")
y += 60
# === 板块内容 ===
for section_idx, section in enumerate(sections):
category = section['category']
articles = section['articles']
# 板块背景色
cat_color = CATEGORY_COLORS.get(category, PAPER_DARK)
# 板块标题区域
section_y = y
draw.rectangle([MARGIN, y, WIDTH-MARGIN, y+40], fill=cat_color, outline=BORDER_COLOR)
# 板块图标(用色块代替)
icon_colors = ["#c9a84c", "#8b0000", "#1a3a5c", "#2e7d32", "#6a1b9a", "#d84315"]
icon_color = icon_colors[section_idx % len(icon_colors)]
draw.rectangle([MARGIN+10, y+8, MARGIN+30, y+32], fill=icon_color)
# 板块标题
draw.text((MARGIN+40, y+20), f"【{category}】", fill=TEXT_BLACK, font=font_headline, anchor="lm")
y += 50
# 文章列表
for article in articles:
title = article['title']
desc = article['desc']
# 文章标题(带序号)
title_text = f"• {title}"
draw.text((MARGIN+15, y), title_text, fill=TEXT_BLACK, font=font_bold)
y += 22
# 描述(缩进,自动换行)
if desc:
desc_text = desc[:80] + "..." if len(desc) > 80 else desc
y = draw_text_wrapped(
draw, desc_text,
MARGIN+35, y,
WIDTH - 2*MARGIN - 50,
font_regular, TEXT_BLACK,
line_height=18
)
y += 8
y += 5
y += 15
# 板块分隔线
if section_idx < len(sections) - 1:
draw.line([(MARGIN+20, y), (WIDTH-MARGIN-20, y)], fill=BORDER_COLOR, width=1)
y += 15
# === 页脚 ===
y += 20
footer_text = f"由 AI 智能体生成 | {datetime.now().strftime('%H:%M')} | 数据来源:{group_name}"
draw.text((WIDTH//2, y), footer_text, fill=TEXT_BLACK, font=font_small, anchor="mm")
# 保存
img.save(output_path, quality=95)
print(f"Newspaper infographic saved: {output_path}")
print(f"Size: {WIDTH}x{total_height}, Sections: {len(sections)}, Articles: {total_articles}")
return output_path
def main():
parser = argparse.ArgumentParser(description='Generate newspaper-style infographic')
parser.add_argument('--report', required=True, help='Path to report markdown file')
parser.add_argument('--output', default='/sdcard/WeChatDigest/infographic.png',
help='Output image path')
parser.add_argument('--group', default='8-TG0 AI AGI', help='Group name')
args = parser.parse_args()
# 读取报告
with open(args.report, 'r', encoding='utf-8') as f:
report_text = f.read()
# 生成信息图
create_newspaper_infographic(report_text, args.output, args.group)
if __name__ == '__main__':
main()