Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FerranAD/a394c672453ecf1e81a4527648200ac3 to your computer and use it in GitHub Desktop.
Save FerranAD/a394c672453ecf1e81a4527648200ac3 to your computer and use it in GitHub Desktop.
import csv
import os
import uuid
import requests
from urllib.parse import urlparse
def download_images(csv_file):
if not os.path.exists("downloaded_images"):
os.makedirs("downloaded_images")
with open(csv_file, 'r') as file:
reader = csv.DictReader(file)
for row_num, row in enumerate(reader, start=1):
folder_name = str(uuid.uuid4())
folder_path = os.path.join("downloaded_images", folder_name)
os.makedirs(folder_path)
for i, url in enumerate(row.values()):
try:
response = requests.get(url)
if response.status_code == 200:
# Get the image name from the URL
image_name = os.path.splitext(os.path.basename(urlparse(url).path))[0]
with open(os.path.join(folder_path, f"{image_name}.jpg"), 'wb') as img_file:
img_file.write(response.content)
print(f"Image {image_name} downloaded successfully for row {folder_name}")
else:
print(f"Failed to download image for row {folder_name}, URL: {url}. Status code: {response.status_code}")
except Exception as e:
print(f"Error downloading image for row {folder_name}, URL: {url}, Row number: {row_num}. Error: {str(e)}")
if __name__ == "__main__":
csv_file = "dataset.csv"
download_images(csv_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment