Files
084_sort/test.py
2026-04-10 20:17:38 +08:00

59 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import json
# 设置基础路径 - 根据你的实际路径修改
base_path = "D:/progarm/python/084PlayHtml/images"
# 定义罪人数据 - 从你的gameData中提取的imgName
sinners = [
"yisang",
"faust",
"don_quixote",
"yoshihide",
"meursault",
"honglu",
"heathcliff",
"ishmael",
"rodya",
"dante",
"sinclair",
"outis",
"gregor"
]
# 初始化结果列表
result = []
# 遍历每个罪人
for sinner in sinners:
dir_path = os.path.join(base_path, sinner)
# 检查目录是否存在
if os.path.isdir(dir_path):
# 获取目录中的所有PNG图片
images = []
for file_name in os.listdir(dir_path):
if file_name.endswith('.webp'): # 如果不是PNG修改这里
# 计算相对路径
relative_path = f"images/{sinner}/{file_name}"
images.append({
"name": os.path.splitext(file_name)[0],
"path": relative_path
})
# 添加到结果列表
result.append({
"name": sinner,
"images": images
})
else:
print(f"警告: 目录不存在: {dir_path}")
# 保存为JSON文件
json_path = os.path.join(base_path, "sinner_images.json")
with open(json_path, "w", encoding="utf-8") as f:
json.dump(result, f, indent=2, ensure_ascii=False)
print(f"JSON文件已生成: {json_path}")