-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomic_continue.py
More file actions
323 lines (260 loc) · 11.5 KB
/
Copy pathcomic_continue.py
File metadata and controls
323 lines (260 loc) · 11.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
漫画续写器 - 使用配置文件
自动检测最新章节并续写漫画
"""
import sys
import json
import time
import shutil
from pathlib import Path
from datetime import datetime
from typing import Dict, Any, Optional, List
project_root = Path(__file__).parent
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from markflow.cli.commands import execute_skill
from comic_utils import ComicConfig, ComicLogger, get_image_paths, config as global_config
class ComicContinuer:
"""漫画续写器 - 使用配置文件"""
def __init__(self, config: ComicConfig = None):
self.config = config or global_config
self.project_root = project_root
# 目录配置
self.novel_dir = self.project_root / "skills/content/novel_writer/output/novels"
self.script_dir = self.project_root / "skills/comics/manga_script_writer/output"
self.image_dir = self.project_root / "skills/image/sd_image_generator/output/images"
self.bubble_dir = self.project_root / "skills/comics/manga_bubble_adder/output"
self.output_dir = self.project_root / "output/comic_continued"
self.output_dir.mkdir(parents=True, exist_ok=True)
# ✅ 先定义 title
self.title = self.config.get('project.name', '漫画')
# ✅ 再加载状态
self.state_file = self.output_dir / "state.json"
self.state = self._load_state()
def _load_state(self) -> Dict:
"""加载续写状态"""
if self.state_file.exists():
try:
with open(self.state_file, 'r', encoding='utf-8') as f:
return json.load(f)
except:
pass
return {
"last_chapter": 0,
"processed_novels": [],
"title": self.title,
"total_pages": 0
}
def _save_state(self):
"""保存续写状态"""
self.state['title'] = self.title
with open(self.state_file, 'w', encoding='utf-8') as f:
json.dump(self.state, f, ensure_ascii=False, indent=2)
def _get_latest_novel(self) -> Optional[Path]:
"""获取最新未处理的小说"""
novels = sorted(self.novel_dir.glob("zh_*.txt"),
key=lambda p: p.stat().st_mtime, reverse=True)
for n in novels:
if str(n) not in self.state.get('processed_novels', []):
return n
return None
def _continue_novel(self, chapters: int = None) -> Optional[Path]:
"""续写小说"""
ComicLogger.info(f"续写小说", "步骤1")
if chapters is None:
chapters = self.config.get('novel.chapters', 3)
novel_config = self.config.novel_config
novel_file = self._get_latest_novel()
result = execute_skill(
"novel_writer",
genre=novel_config.get('genre', '科幻'),
title=novel_config.get('title', self.title),
outline=novel_config.get('outline', ''),
characters=novel_config.get('characters', ''),
chapter_count=chapters,
continue_from=str(novel_file) if novel_file else "",
language=novel_config.get('language', 'zh')
)
if result and result.get('status') == 'success':
result_data = result.get('result', {})
saved_to = result_data.get('saved_to')
if saved_to and Path(saved_to).exists():
ComicLogger.success(f"小说续写完成: {saved_to}", "步骤1")
self.state['last_chapter'] += chapters
return Path(saved_to)
ComicLogger.error("小说续写失败", "步骤1")
return None
def _continue_comic(self, novel_file: Path, pages: int = None) -> Dict:
"""从小说续写漫画"""
ComicLogger.info(f"从小说生成漫画续集: {novel_file.name}", "步骤2")
if pages is None:
pages = self.config.get('manga.pages', 4)
manga_config = self.config.manga_config
# 1. 生成剧本
result = execute_skill(
"manga_script_writer",
novel_file=str(novel_file),
pages=pages
)
if not result or result.get('status') != 'success':
ComicLogger.error("剧本生成失败", "步骤2")
return {}
script_path = result.get('output_path')
ComicLogger.success(f"剧本: {script_path}", "步骤2")
# 2. 生成图片
result = execute_skill(
"manga_generator",
script_path=script_path,
style=manga_config.get('style', 'manga'),
pages=pages,
steps=manga_config.get('steps', 30),
strength=manga_config.get('strength', 0.65)
)
if not result or result.get('status') != 'success':
ComicLogger.error("图片生成失败", "步骤2")
return {}
# 获取最新图片
images = sorted(self.image_dir.glob("image_*.png"),
key=lambda p: p.stat().st_mtime, reverse=True)[:pages]
if not images:
ComicLogger.error("没有找到生成的图片", "步骤2")
return {}
ComicLogger.success(f"生成了 {len(images)} 张图片", "步骤2")
# 3. 添加气泡
bubbled_paths = []
for i, img_path in enumerate(images):
dialogues = self.config.get_dialogues(i)
positions = self.config.get_positions()
result = execute_skill(
"manga_bubble_adder",
image_path=str(img_path),
dialogues=dialogues,
positions=positions[:len(dialogues)],
bubble_style=self.config.get('bubbles.bubble_style', 'rounded')
)
if result and result.get('status') == 'success':
output = result.get('output_path')
if output:
bubbled_paths.append(output)
ComicLogger.success(f"添加了 {len(bubbled_paths)} 个气泡", "步骤2")
# 4. 导出
if bubbled_paths:
export_config = self.config.export_config
formats = export_config.get('formats', ['pdf'])
# 合并所有图片(原有 + 新生成的)
all_images = get_image_paths("bubbled_*.png", self.bubble_dir)
results = {}
if 'pdf' in formats:
result = execute_skill(
"manga_to_pdf",
image_paths=all_images,
title=f"{self.title} (续)",
page_size=export_config.get('page_size', 'A4')
)
if result and result.get('status') == 'success':
results['pdf'] = result.get('output_path')
ComicLogger.success(f"PDF: {results['pdf']}", "步骤2")
if 'epub' in formats:
result = execute_skill(
"manga_to_epub",
image_paths=all_images,
title=f"{self.title} (续)",
author=export_config.get('author', 'AI 生成')
)
if result and result.get('status') == 'success':
results['epub'] = result.get('output_path')
ComicLogger.success(f"EPUB: {results['epub']}", "步骤2")
# 保存续集
continued_dir = self._save_continued(results, bubbled_paths)
return {
"script": script_path,
"images": images,
"bubbled": bubbled_paths,
"results": results,
"output_dir": continued_dir
}
return {}
def _save_continued(self, results: dict, bubbled_paths: list) -> Path:
"""保存续集"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
continued_dir = self.output_dir / f"续写_{timestamp}"
continued_dir.mkdir(exist_ok=True)
# 复制 PDF
if results.get('pdf') and Path(results['pdf']).exists():
shutil.copy(results['pdf'], continued_dir / "comic.pdf")
# 复制 EPUB
if results.get('epub') and Path(results['epub']).exists():
shutil.copy(results['epub'], continued_dir / "comic.epub")
# 复制图片
img_dir = continued_dir / "images"
img_dir.mkdir(exist_ok=True)
for img in bubbled_paths:
shutil.copy(img, img_dir / Path(img).name)
# 保存状态
state_path = continued_dir / "state.json"
with open(state_path, 'w', encoding='utf-8') as f:
json.dump({
"generated_at": datetime.now().isoformat(),
"pages": len(bubbled_paths),
"title": self.title
}, f, ensure_ascii=False, indent=2)
ComicLogger.success(f"续集已保存: {continued_dir}", "输出")
return continued_dir
def run(self, chapters: int = None, pages: int = None):
"""运行续写流程"""
if chapters is None:
chapters = self.config.get('novel.chapters', 3)
if pages is None:
pages = self.config.get('manga.pages', 4)
print("\n" + "=" * 60)
print(" 🔄 漫画续写器")
print("=" * 60)
print(f"📖 标题: {self.title}")
print(f"📄 续写章节: {chapters}")
print(f"🎨 生成页数: {pages}")
print(f"📊 已处理: {len(self.state.get('processed_novels', []))} 部小说")
print("=" * 60)
# 1. 续写小说
novel_file = self._continue_novel(chapters)
if not novel_file:
ComicLogger.error("续写失败,退出")
return
# 2. 生成漫画续集
result = self._continue_comic(novel_file, pages)
if result:
self.state['processed_novels'].append(str(novel_file))
self.state['total_pages'] += pages
self._save_state()
print("\n" + "=" * 60)
print(" ✅ 续写完成!")
print("=" * 60)
if result.get('output_dir'):
print(f"📂 输出: {result['output_dir']}")
print("=" * 60)
def main():
import argparse
parser = argparse.ArgumentParser(description="漫画续写器")
parser.add_argument("--config", help="配置文件路径(可选)") # 去掉 -c
parser.add_argument("--chapters", "-n", type=int, help="续写章节数") # 改为 -n
parser.add_argument("--pages", "-p", type=int, help="生成页数")
parser.add_argument("--status", "-s", action="store_true", help="显示当前状态")
args = parser.parse_args()
# 加载配置
if args.config:
config = ComicConfig(Path(args.config))
else:
config = global_config
continuer = ComicContinuer(config)
if args.status:
state = continuer.state
print("\n📊 续写状态:")
print(f" 标题: {state.get('title', '未知')}")
print(f" 上次章节: {state.get('last_chapter', 0)}")
print(f" 已处理小说: {len(state.get('processed_novels', []))}")
print(f" 总页数: {state.get('total_pages', 0)}")
return
continuer.run(args.chapters, args.pages)
if __name__ == "__main__":
main()