File size: 853 Bytes
4ee31f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import argparse
import csv
import time
from pathlib import Path
from urllib.request import urlretrieve

wait_time = 1.0


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Image Downloader")
    parser.add_argument("--csv", type=str, required=True, help="Path to CSV file of images to download")
    parser.add_argument("--output", type=str, required=True, help="Path to output directory")
    args = parser.parse_args()

    output_dir = Path(args.output)
    output_dir.mkdir(parents=True, exist_ok=True)

    with open(args.csv, "r") as f:
        reader = csv.reader(f)
        for i, row in enumerate(reader):
            url = row[3]
            id_ = row[0]
            try:
                urlretrieve(url, output_dir / id_)
            except:
                print(f"Failed to download {url}")
            time.sleep(wait_time)