import os, json

# 输入输出目录
in_dir  = "/Users/zongyikun/Desktop/qwen_vl_demo/kilogram-main/dataset/params_from_svg"
out_dir = "/Users/zongyikun/Desktop/qwen_vl_demo/kilogram-main/dataset/onepiece_from_svg"
os.makedirs(out_dir, exist_ok=True)

# 遍历所有 json 文件
for fname in os.listdir(in_dir):
    if not fname.endswith(".json"):
        continue
    path = os.path.join(in_dir, fname)
    with open(path, "r") as f:
        data = json.load(f)

    # 假设结构里有 "pieces" 字段，里面是一个 list
    if "pieces" not in data:
        continue
    pieces = data["pieces"]

    base = os.path.splitext(fname)[0]
    for i, piece in enumerate(pieces):
        out_path = os.path.join(out_dir, f"{base}_piece{i+1}.json")
        with open(out_path, "w") as f:
            json.dump(piece, f, indent=2)

print("✅ Done! 所有 7 个 piece 已拆分成单独 JSON，存放在", out_dir)
import os, json
from typing import List, Dict, Any

# 输入/输出目录（绝对路径，避免 cwd 问题）
IN_DIR  = "/Users/zongyikun/Desktop/qwen_vl_demo/kilogram-main/dataset/params_from_svg"
OUT_DIR = "/Users/zongyikun/Desktop/qwen_vl_demo/kilogram-main/dataset/onepiece_from_svg"

os.makedirs(OUT_DIR, exist_ok=True)


def extract_pieces(obj: Any) -> List[Dict[str, Any]]:
    """根据不同 JSON 结构提取 pieces 列表。
    支持：
      1) 顶层就是 list（你的 page*.json 当前就是这种）
      2) 有 data["pieces"]
      3) 有 data["params"]
    其它结构直接返回空列表。
    """
    if isinstance(obj, list):
        return obj
    if isinstance(obj, dict):
        if "pieces" in obj and isinstance(obj["pieces"], list):
            return obj["pieces"]
        if "params" in obj and isinstance(obj["params"], list):
            return obj["params"]
    return []


def main():
    total_files = 0
    total_pieces = 0

    for fname in sorted(os.listdir(IN_DIR)):
        if not fname.endswith(".json"):
            continue
        path = os.path.join(IN_DIR, fname)
        try:
            with open(path, "r") as f:
                data = json.load(f)
        except Exception as e:
            print(f"[WARN] 读取失败: {path} -> {e}")
            continue

        pieces = extract_pieces(data)
        if not pieces:
            print(f"[SKIP] 未找到可拆分的 pieces: {fname}")
            continue

        base = os.path.splitext(fname)[0]
        for i, piece in enumerate(pieces, start=1):
            # 取出类型用于命名（可选）
            ptype = None
            if isinstance(piece, dict):
                ptype = piece.get("type")
            suffix = f"_piece{i}"
            if ptype:
                suffix += f"_{ptype}"

            out_path = os.path.join(OUT_DIR, f"{base}{suffix}.json")
            try:
                with open(out_path, "w") as fo:
                    json.dump(piece, fo, indent=2, ensure_ascii=False)
            except Exception as e:
                print(f"[WARN] 写入失败: {out_path} -> {e}")
                continue

            total_pieces += 1
        total_files += 1
        print(f"[OK] {fname}: 拆分 {len(pieces)} 个 -> {OUT_DIR}")

    print(f"\n✅ Done! 处理文件数: {total_files}, 生成单块 JSON 数: {total_pieces}")


if __name__ == "__main__":
    main()