FFHQ-Aging-Dataset / src /zip_images.py
PIEthonista's picture
update data
c9d0dcd
"""
Zip a (possibly nested) directory into multiple zip archives, each with CHUNK_SIZE files.
Unzipping any archive will recreate the original relative structure under INPUT_DIR.
"""
import os
from pathlib import Path
import zipfile
from tqdm import tqdm
from itertools import islice
# === CONFIG ===================================================
# ==============================================================
CHUNK_SIZE = 1000 # files per zip
EXTS = [".png"] # which file extensions to include. Use ["*"] for everything
INPUT_2_ZIP_DIRS = {
"images/images1024x1024": "images_zip/images1024x1024",
"images/custom1024x1024": "images_zip/custom1024x1024",
"images/custom256x256_matte_white_background": "images_zip/custom256x256_matte_white_background",
"images/custom512x512_matte_white_background": "images_zip/custom512x512_matte_white_background",
"images/custom1024x1024_skin_tones": "images_zip/custom1024x1024_skin_tones",
"images/images512x512_matte_white_background_categorised": "images_zip/images512x512_matte_white_background_categorised",
"images/images512x512_matte_white_background_similars": "images_zip/images512x512_matte_white_background_similars",
"segmaps/custom1024x1024": "segmaps_zip/custom1024x1024",
"segmaps/segmaps512x512": "segmaps_zip/segmaps512x512",
}
# ==============================================================
# ==============================================================
# === HELPERS ===
def batched(iterable, n):
it = iter(iterable)
while True:
batch = list(islice(it, n))
if not batch:
break
yield batch
def want_file(p: Path) -> bool:
return "*" in EXTS or p.suffix.lower() in EXTS
# === SCRIPT ===
for INPUT_DIR, ZIP_DIR in tqdm(INPUT_2_ZIP_DIRS.items()):
INPUT_DIR = Path(INPUT_DIR)
ZIP_DIR = Path(ZIP_DIR)
ZIP_DIR.mkdir(parents=True, exist_ok=True)
# Collect all matching files recursively
all_files = sorted([p for p in INPUT_DIR.rglob("*") if p.is_file() and want_file(p)])
for idx, chunk in enumerate(tqdm(list(batched(all_files, CHUNK_SIZE)), desc="Chunks")):
zip_path = ZIP_DIR / f"images_part_{idx:05}.zip"
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
for f in tqdm(chunk, desc="Files in chunk", leave=False):
# path inside zip (relative to INPUT_DIR)
arcname = f.relative_to(INPUT_DIR)
zf.write(f, arcname)
print(f"Created: {zip_path} with {len(chunk)} files")
print("DONE.")