import os import time import requests from concurrent.futures import ThreadPoolExecutor # Directory where images will be saved OUTPUT_DIR = os.path.join("data", "faces") os.makedirs(OUTPUT_DIR, exist_ok=True) # URL serves a new face on every GET request URL = "https://thispersondoesnotexist.com/random-person.jpeg" # Set a standard User-Agent header to prevent requests from being blocked HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" } NUM_IMAGES = 700 DELAY_SECONDS = 0.00 # Polite delay between requests per worker NUM_WORKERS = 6 def download_face(image_index: int) -> None: """Worker function to download a single face image.""" file_path = os.path.join(OUTPUT_DIR, f"face_{image_index:03d}.jpg") try: response = requests.get(URL, headers=HEADERS, timeout=10) if response.status_code == 200: with open(file_path, "wb") as f: f.write(response.content) print(f"[{image_index}/{NUM_IMAGES}] Saved: {file_path}") else: print( f"[{image_index}/{NUM_IMAGES}] Failed with status code: {response.status_code}" ) except Exception as e: print(f"[{image_index}/{NUM_IMAGES}] Error: {e}") time.sleep(DELAY_SECONDS) def main(): print( f"Starting download of {NUM_IMAGES} faces using {NUM_WORKERS} workers..." ) with ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor: executor.map(download_face, range(1, NUM_IMAGES + 1)) print("Download complete!") if __name__ == "__main__": main()