File size: 885 Bytes
81f161c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
import os, tarfile, math, json
from pathlib import Path

IMAGE_DIR   = Path("images")          # all PNGs live here now
META_JSON   = Path("metadata.json")
PER_SHARD   = 10_000                  # ≤ 10 000 files per tar
OUT_DIR     = Path(".")               # tars are written next to script

# 1. load metadata so we keep ordering consistent
with META_JSON.open() as f:
    meta = json.load(f)
all_names = sorted(meta)

n_shards = math.floor((len(all_names)-1) / PER_SHARD) + 1
for s in range(n_shards):
    start = s * PER_SHARD
    end   = min((s+1)*PER_SHARD - 1, len(all_names) - 1)
    shard_name = OUT_DIR / f"images-{start:05d}-{end:05d}.tar"
    print(f" → building {shard_name}")
    with tarfile.open(shard_name, "w") as tar:
        for fname in all_names[start:end+1]:
            tar.add(IMAGE_DIR / fname, arcname=fname)