File size: 1,128 Bytes
4ee31f3
 
 
 
 
 
 
 
 
 
 
8c24bc5
 
 
 
 
 
 
 
 
 
 
 
4ee31f3
 
 
 
 
 
 
8c24bc5
4ee31f3
 
 
8c24bc5
4ee31f3
8c24bc5
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
        next(reader)
        for i, row in enumerate(reader):
            url = row[3]
            id_ = row[0]
            img_format = url.split(".")[-1]
            try:
                urlretrieve(url, output_dir / f"{id_}.{img_format}")
                if (i + 1) % 100 == 0:
                    print(f"Downloaded {i + 1} images")
            except Exception as e:
                print(f"{e} : Failed to download {url}")
            time.sleep(wait_time)