Dealing with a image dataset? Dealing with CSVs intead of JPGs? Use this script to download images from a CSV file, which were originally stored as URLs.
To download full resolution images, type:
$ python download-images-from-csv.py <csv_filename>
To download thumbnail images, type:
$ python download-thumbnails-from-csv.py <csv_filename>
$ python download-images-from-csv.py images
Assuming images.csv has the following columns:
- Image Name (ImageID) in column 1
- Full Resolution URL (OriginalURL) in column 3
$ python download-thumbnails-from-csv.py images
Assuming images.csv has the following columns:
- Image Name (ImageID) in column 1
- Thumbnail URL (Thumbnail300KURL) in column 11
Full resolution images are stored into fullres
folder, as <ImageID>.jpg
Thumbnail images are stored into thumbnails
folder, as <ImageID>.jpg
For python3 use:
`import sys
from csv import reader
import os.path
import urllib.request
csv_filename = sys.argv[1]
with open(csv_filename+".csv".format(csv_filename), 'r') as csv_file:
for line in reader(csv_file):
if os.path.isfile("fullres/" + line[0] + ".jpg"):
print ("Image skipped for {0}".format(line[0]))
else:
if line[2] != '' and line[0] != "ImageID":
urllib.request.urlretrieve(line[2], "fullres/" + line[0] + ".jpg")
print ("Image saved for {0}".format(line[0]))
else:
print ("No result for {0}".format(line[0]))`