wechat-media-writer 2.2.12 → 2.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/SKILL.md
CHANGED
|
@@ -219,11 +219,22 @@ urls = download_theme_images("books", "/tmp/images", count=6)
|
|
|
219
219
|
3. **分辨率**:宽高乘积
|
|
220
220
|
4. **色彩多样性**:RGB 通道差值(避免纯灰/单色图)
|
|
221
221
|
|
|
222
|
-
|
|
222
|
+
### 内容图比例要求
|
|
223
|
+
|
|
224
|
+
公众号正文配图应使用**横向图**(宽 > 高),与微信文章阅读体验最佳:
|
|
225
|
+
|
|
226
|
+
| 优先级 | 比例 | 备注 |
|
|
227
|
+
| --- | --- | --- |
|
|
228
|
+
| ⭐⭐⭐ 优先 | 4:3 (1.33) | 视觉最舒适,公众号默认容器适配 |
|
|
229
|
+
| ⭐⭐ 次选 | 3:2 (1.50) | 经典相机比例,普适性强 |
|
|
230
|
+
| ⭐ 备选 | 16:9 (1.78) | 偏宽,电影感 |
|
|
231
|
+
| ❌ 避免 | 竖向 (h > w) | 微信展示高度 > 600px 显突兀 |
|
|
232
|
+
|
|
233
|
+
下载时会自动跳过竖向图和比例异常的候选。下载 6 张主题图后会自动按美学评分降序重命名为 `img_1.jpg` ~ `img_6.jpg`,
|
|
223
234
|
其中 `img_1.jpg` 就是评分最高的,被 `pick_best_cover()` 选中裁剪为 900x500 封面。
|
|
224
235
|
|
|
225
236
|
**支持的主题**:`abstract` / `books` / `nature` / `technology` / `business`
|
|
226
|
-
每个主题预存 8 张精选 Pexels 候选(见 `THEME_CANDIDATES
|
|
237
|
+
每个主题预存 8 张精选 Pexels 候选(见 `THEME_CANDIDATES`),按 4:3 > 3:2 > 16:9 优先级排序。
|
|
227
238
|
|
|
228
239
|
### 第三步:上传图片到微信
|
|
229
240
|
|
package/package.json
CHANGED
|
Binary file
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"""
|
|
3
3
|
图片下载模块
|
|
4
4
|
- 每个主题预存 8-10 张精选 Pexels 候选 ID(已实地测试可用)
|
|
5
|
-
- 下载时跳过 404
|
|
6
|
-
- 提供
|
|
5
|
+
- 下载时跳过 404 候选,并校验宽高比(优先 4:3 / 3:2 横向图)
|
|
6
|
+
- 提供 aesthetics_score() 函数评估图片"精美度",便于封面自动挑选
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
import os
|
|
@@ -14,20 +14,23 @@ from PIL import Image, ImageStat
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
# 精选 Pexels 候选 ID(每个主题 8-10 张,实地下载验证)
|
|
17
|
-
#
|
|
17
|
+
# 选择标准:
|
|
18
|
+
# 1) 比例优先 4:3 (1.33) 其次 3:2 (1.5) 横向构图(宽 > 高)
|
|
19
|
+
# 2) 主题契合(书评/影评主题)
|
|
20
|
+
# 3) 构图饱满、色彩丰富、画面精美
|
|
18
21
|
THEME_CANDIDATES = {
|
|
19
22
|
"books": [
|
|
20
|
-
#
|
|
21
|
-
415078, # 草地上开本书(封面候选极佳)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
# 横向 3:2 主力 - 已实地下载验证为"读书/书"主题
|
|
24
|
+
415078, # 3:2 - 草地上开本书(封面候选极佳)
|
|
25
|
+
904616, # 3:2 - 咖啡+书+花俯拍
|
|
26
|
+
1319854, # 3:2 - 图书馆书墙(精装)
|
|
27
|
+
4974915, # 3:2 - 翻书动作
|
|
28
|
+
2228547, # 3:2 - 翻书
|
|
29
|
+
1370295, # 3:2 - 开本书
|
|
30
|
+
2099683, # 16:9 - 翻书动作
|
|
31
|
+
# 已剔除:以下 ID 在 Pexels 搜索结果中出现但主题不契合(风景、人物特写、粉背景等)
|
|
32
|
+
# 4239031 (电脑前背影)、36729918 (粉背景酒杯)、2820896 (街舞)、
|
|
33
|
+
# 1444442 (手部彩绘)、261949 (报纸)、1438072 (笔记本咖啡不够书主题)
|
|
31
34
|
],
|
|
32
35
|
"abstract": [
|
|
33
36
|
1108572, # 抽象光影
|
|
@@ -164,6 +167,30 @@ def aesthetics_score(image_path):
|
|
|
164
167
|
return round(total, 2)
|
|
165
168
|
|
|
166
169
|
|
|
170
|
+
def is_valid_landscape(image_path, min_ratio=1.2, max_ratio=2.0):
|
|
171
|
+
"""校验图片是横向图且比例在合理范围
|
|
172
|
+
|
|
173
|
+
横向比例:1.2 ~ 2.0
|
|
174
|
+
排除:竖向图、纯单色图(细节过低)
|
|
175
|
+
"""
|
|
176
|
+
try:
|
|
177
|
+
img = Image.open(image_path)
|
|
178
|
+
w, h = img.size
|
|
179
|
+
if w < h:
|
|
180
|
+
return False
|
|
181
|
+
ratio = w / h
|
|
182
|
+
if not (min_ratio <= ratio <= max_ratio):
|
|
183
|
+
return False
|
|
184
|
+
# 校验不是纯色图(stddev 极低 = 纯色/几乎无细节)
|
|
185
|
+
stat = ImageStat.Stat(img.convert("RGB"))
|
|
186
|
+
avg_stddev = sum(stat.stddev) / 3
|
|
187
|
+
if avg_stddev < 15: # 几乎纯色,丢弃
|
|
188
|
+
return False
|
|
189
|
+
return True
|
|
190
|
+
except Exception:
|
|
191
|
+
return False
|
|
192
|
+
|
|
193
|
+
|
|
167
194
|
def download_theme_images(theme, output_dir, count=6):
|
|
168
195
|
"""根据主题下载多张精美图片
|
|
169
196
|
|
|
@@ -176,13 +203,24 @@ def download_theme_images(theme, output_dir, count=6):
|
|
|
176
203
|
downloaded = [] # 临时文件列表
|
|
177
204
|
failed_pids = []
|
|
178
205
|
|
|
179
|
-
# 1)
|
|
206
|
+
# 1) 全部下载到临时文件(仅保留横向图:1.2 ≤ ratio ≤ 2.0)
|
|
180
207
|
for i, pid in enumerate(candidates):
|
|
181
208
|
if len(downloaded) >= count:
|
|
182
209
|
break
|
|
183
210
|
tmp_path = os.path.join(output_dir, f"_tmp_{theme}_{i}_{pid}.jpg")
|
|
184
211
|
if download_pexels_image(pid, tmp_path):
|
|
185
|
-
|
|
212
|
+
if is_valid_landscape(tmp_path):
|
|
213
|
+
downloaded.append(tmp_path)
|
|
214
|
+
else:
|
|
215
|
+
# 不是横向图或纯色图,丢弃
|
|
216
|
+
try:
|
|
217
|
+
img = Image.open(tmp_path)
|
|
218
|
+
w, h = img.size
|
|
219
|
+
ratio = w / h if h > 0 else 0
|
|
220
|
+
print(f" 跳过 {pid}({w}x{h},比例 {ratio:.2f} 不符合横向规范)")
|
|
221
|
+
except Exception:
|
|
222
|
+
print(f" 跳过 {pid}(无法校验)")
|
|
223
|
+
os.remove(tmp_path)
|
|
186
224
|
else:
|
|
187
225
|
failed_pids.append(pid)
|
|
188
226
|
|