重新生成全部配图
This commit is contained in:
parent
dc981053fe
commit
0ffc31ae3e
@ -28,8 +28,17 @@ def load_config(config_path="poster_gen_config.json"):
|
|||||||
logging.error(f"加载配置文件时出错: {e}")
|
logging.error(f"加载配置文件时出错: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def regenerate_posters_and_images(run_dir: str, config: Dict):
|
def regenerate_posters_and_images(run_dir: str, config: Dict, target_topic: int = None,
|
||||||
"""为所有主题重新生成海报和配图"""
|
ignore_metadata: bool = False, poster_config_path: str = None):
|
||||||
|
"""为所有主题重新生成海报和配图
|
||||||
|
|
||||||
|
Args:
|
||||||
|
run_dir: 运行目录路径
|
||||||
|
config: 配置字典
|
||||||
|
target_topic: 只处理特定主题ID
|
||||||
|
ignore_metadata: 是否忽略现有的元数据
|
||||||
|
poster_config_path: 海报配置文件路径
|
||||||
|
"""
|
||||||
run_id = os.path.basename(run_dir)
|
run_id = os.path.basename(run_dir)
|
||||||
logging.info(f"处理运行ID: {run_id}")
|
logging.info(f"处理运行ID: {run_id}")
|
||||||
|
|
||||||
@ -51,6 +60,17 @@ def regenerate_posters_and_images(run_dir: str, config: Dict):
|
|||||||
logging.error("无法加载主题列表")
|
logging.error("无法加载主题列表")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# 如果指定了poster_config_path,加载自定义海报配置
|
||||||
|
poster_configs = None
|
||||||
|
if poster_config_path:
|
||||||
|
try:
|
||||||
|
with open(poster_config_path, 'r', encoding='utf-8') as f:
|
||||||
|
poster_configs = json.load(f)
|
||||||
|
logging.info(f"已加载自定义海报配置,包含 {len(poster_configs)} 个配置项")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"加载海报配置文件时出错: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
# 加载配置参数
|
# 加载配置参数
|
||||||
poster_variants = config.get("variants", 1)
|
poster_variants = config.get("variants", 1)
|
||||||
poster_assets_dir = config.get("poster_assets_base_dir")
|
poster_assets_dir = config.get("poster_assets_base_dir")
|
||||||
@ -90,8 +110,48 @@ def regenerate_posters_and_images(run_dir: str, config: Dict):
|
|||||||
logging.warning(f"主题缺少索引,跳过: {topic_item}")
|
logging.warning(f"主题缺少索引,跳过: {topic_item}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 如果指定了target_topic且不匹配当前主题,则跳过
|
||||||
|
if target_topic is not None and topic_index != target_topic:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 检查是否有自定义海报配置
|
||||||
|
custom_config = None
|
||||||
|
if poster_configs:
|
||||||
|
for config_item in poster_configs:
|
||||||
|
if config_item.get('index') == topic_index:
|
||||||
|
custom_config = config_item
|
||||||
|
break
|
||||||
|
|
||||||
|
if custom_config:
|
||||||
|
# 更新主题标题
|
||||||
|
if 'main_title' in custom_config:
|
||||||
|
topic_item['title'] = custom_config['main_title']
|
||||||
|
logging.info(f"使用自定义标题: {custom_config['main_title']}")
|
||||||
|
|
||||||
logging.info(f"处理主题 {topic_index}: {topic_item.get('object', 'N/A')}")
|
logging.info(f"处理主题 {topic_index}: {topic_item.get('object', 'N/A')}")
|
||||||
|
|
||||||
|
# 如果忽略元数据,先清空所有现有的海报和配图文件
|
||||||
|
if ignore_metadata:
|
||||||
|
for variant_index in range(1, poster_variants + 1):
|
||||||
|
variant_key = f"{topic_index}_{variant_index}"
|
||||||
|
topic_dir = os.path.join(run_dir, variant_key)
|
||||||
|
|
||||||
|
if os.path.exists(topic_dir):
|
||||||
|
poster_dir = os.path.join(topic_dir, poster_subdir)
|
||||||
|
if os.path.exists(poster_dir):
|
||||||
|
logging.info(f"清空变体 {variant_key} 的海报目录")
|
||||||
|
for file_name in os.listdir(poster_dir):
|
||||||
|
file_path = os.path.join(poster_dir, file_name)
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
os.remove(file_path)
|
||||||
|
logging.debug(f"已删除文件: {file_path}")
|
||||||
|
else:
|
||||||
|
os.makedirs(poster_dir, exist_ok=True)
|
||||||
|
else:
|
||||||
|
os.makedirs(topic_dir, exist_ok=True)
|
||||||
|
os.makedirs(os.path.join(topic_dir, poster_subdir), exist_ok=True)
|
||||||
|
logging.info(f"创建变体目录: {topic_dir}")
|
||||||
|
|
||||||
# 1. 为此主题生成所有变体的海报(一次性调用)
|
# 1. 为此主题生成所有变体的海报(一次性调用)
|
||||||
logging.info(f"为主题 {topic_index} 生成所有变体的海报...")
|
logging.info(f"为主题 {topic_index} 生成所有变体的海报...")
|
||||||
|
|
||||||
@ -152,6 +212,13 @@ def regenerate_posters_and_images(run_dir: str, config: Dict):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 删除现有的额外配图文件
|
||||||
|
for file_name in os.listdir(poster_dir):
|
||||||
|
if file_name.startswith("additional_") and file_name.endswith(".jpg"):
|
||||||
|
file_path = os.path.join(poster_dir, file_name)
|
||||||
|
os.remove(file_path)
|
||||||
|
logging.debug(f"已删除额外配图: {file_path}")
|
||||||
|
|
||||||
# 查找元数据文件
|
# 查找元数据文件
|
||||||
metadata_files = [f for f in os.listdir(poster_dir) if f.endswith("_metadata.json")]
|
metadata_files = [f for f in os.listdir(poster_dir) if f.endswith("_metadata.json")]
|
||||||
|
|
||||||
@ -189,6 +256,10 @@ def regenerate_posters_and_images(run_dir: str, config: Dict):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(f"生成变体 {variant_key} 额外配图时出错: {e}")
|
logging.exception(f"生成变体 {variant_key} 额外配图时出错: {e}")
|
||||||
|
|
||||||
|
# 如果指定了target_topic但没有找到
|
||||||
|
if target_topic is not None and all(topic.get('index') != target_topic for topic in topics_list):
|
||||||
|
logging.warning(f"未找到指定的主题ID: {target_topic}")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="为所有主题重新生成海报和配图")
|
parser = argparse.ArgumentParser(description="为所有主题重新生成海报和配图")
|
||||||
@ -196,6 +267,8 @@ def main():
|
|||||||
parser.add_argument("--config", default="poster_gen_config.json", help="配置文件路径")
|
parser.add_argument("--config", default="poster_gen_config.json", help="配置文件路径")
|
||||||
parser.add_argument("--debug", action="store_true", help="启用调试日志")
|
parser.add_argument("--debug", action="store_true", help="启用调试日志")
|
||||||
parser.add_argument("--topic", type=int, help="只处理指定主题索引")
|
parser.add_argument("--topic", type=int, help="只处理指定主题索引")
|
||||||
|
parser.add_argument("--ignore-metadata", action="store_true", help="忽略现有的元数据,重新生成所有海报和图片")
|
||||||
|
parser.add_argument("--poster-config", help="指定海报配置文件路径,用于自定义海报内容")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# 设置日志级别
|
# 设置日志级别
|
||||||
@ -216,9 +289,25 @@ def main():
|
|||||||
logging.error(f"指定的运行目录不存在: {args.run_dir}")
|
logging.error(f"指定的运行目录不存在: {args.run_dir}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# 修改regenerate_posters_and_images函数,添加新参数
|
||||||
|
if args.ignore_metadata:
|
||||||
|
logging.info("将忽略现有元数据,完全重新生成所有海报和配图")
|
||||||
|
|
||||||
|
# 如果指定了poster-config
|
||||||
|
if args.poster_config:
|
||||||
|
logging.info(f"使用自定义海报配置文件: {args.poster_config}")
|
||||||
|
# 检查文件是否存在
|
||||||
|
if not os.path.exists(args.poster_config):
|
||||||
|
logging.error(f"指定的海报配置文件不存在: {args.poster_config}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# 开始处理
|
# 开始处理
|
||||||
logging.info(f"开始为 {args.run_dir} 重新生成海报和配图")
|
if args.topic:
|
||||||
regenerate_posters_and_images(args.run_dir, config)
|
logging.info(f"只处理主题 {args.topic} 的海报和配图")
|
||||||
|
else:
|
||||||
|
logging.info(f"开始为 {args.run_dir} 重新生成海报和配图")
|
||||||
|
|
||||||
|
regenerate_posters_and_images(args.run_dir, config, args.topic, args.ignore_metadata, args.poster_config)
|
||||||
logging.info("处理完成")
|
logging.info("处理完成")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user