""" Fetches the 'artwork' domain (WikiArt-derived) images for NoOp-Bench. WikiArt's own terms of use (https://www.wikiart.org/store/terms-of-use) prohibit reproducing or republishing their content on other sites without written consent, so these images are NOT bundled in noopbench_inputs_public.zip. Run this script with your own Kaggle credentials to reconstruct them locally. Requires: pip install kaggle Pillow Requires: ~/.kaggle/kaggle.json with your own Kaggle API credentials (see https://www.kaggle.com/docs/api for how to generate one) Usage: python fetch_artwork.py [--count 2500] [--out data_512_full/artwork] """ import argparse import csv import time import zipfile from pathlib import Path from PIL import Image parser = argparse.ArgumentParser() parser.add_argument("--count", type=int, default=2500, help="number of images to fetch") parser.add_argument("--out", type=str, default="data_512_full/artwork", help="output directory") parser.add_argument("--pacing", type=float, default=1.5, help="seconds between requests") args = parser.parse_args() OUT = Path(args.out) TMP = Path("_tmp_wikiart_download") OUT.mkdir(parents=True, exist_ok=True) TMP.mkdir(parents=True, exist_ok=True) from kaggle import KaggleApi api = KaggleApi() api.authenticate() print("Kaggle auth OK") def resize_file(src, out_path): try: img = Image.open(src).convert("RGB") img = img.resize((512, 512), Image.LANCZOS) img.save(out_path) return True except Exception as e: print(f" [skip] {out_path.name}: {e}") return False def download_kaggle_file(dataset_slug, file_name, dest_dir, max_retries=4, backoff_base=20): # Nested paths (e.g. "Style/artist_title.jpg") need the "/" percent-encoded, # otherwise Kaggle's API routes it as a literal path segment and 404s. api_name = file_name.replace("/", "%2F") for attempt in range(max_retries): try: api.dataset_download_file(dataset_slug, api_name, path=str(dest_dir), force=False, quiet=True) return True except Exception as e: if "429" in str(e) and attempt < max_retries - 1: wait = backoff_base * (attempt + 1) print(f" [429] backing off {wait}s...") time.sleep(wait) continue return False return False def fetch_one(fname, out_idx): out = OUT / f"artwork_wikiart_{out_idx:05d}.png" if out.exists(): return True if not download_kaggle_file("steubk/wikiart", fname, TMP): return False basename = Path(fname).name # Kaggle saves nested-path files with the encoded path baked into the # filename (e.g. "Style%2Fartist_title.jpg"), not a clean basename. downloaded = [p for p in TMP.rglob("*") if p.is_file() and p.name.endswith(basename)] if downloaded: ok = resize_file(downloaded[0], out) downloaded[0].unlink(missing_ok=True) return ok return False existing = len(list(OUT.glob("*.png"))) needed = args.count print(f"artwork: {existing}/{needed} already have") if existing < needed: img_files = [] page_token = None while len(img_files) < needed + 200: resp = api.dataset_list_files("steubk/wikiart", page_token=page_token, page_size=200) batch = [f.name for f in resp.dataset_files if f.name.lower().endswith((".jpg", ".jpeg", ".png"))] img_files.extend(batch) page_token = getattr(resp, "next_page_token", None) if not page_token or not resp.dataset_files: break print(f"listed {len(img_files)} candidate files") manifest_path = Path("artwork_fetch_manifest.csv") with open(manifest_path, "a", newline="") as mf: writer = csv.writer(mf) if manifest_path.stat().st_size == 0: writer.writerow(["out_index", "source_filename"]) done = existing errors = 0 for n, fname in enumerate(img_files[existing:]): if done >= needed: break out_idx = existing + n ok = fetch_one(fname, out_idx) if ok: writer.writerow([out_idx, fname]) mf.flush() done += 1 else: errors += 1 if done % 50 == 0: print(f" artwork: {done}/{needed} (errors so far: {errors})") time.sleep(args.pacing) print(f"artwork done: {len(list(OUT.glob('*.png')))} images") print(f"Manifest of source filenames written to artwork_fetch_manifest.csv")