Created
May 5, 2024 04:21
-
-
Save FerranAD/a394c672453ecf1e81a4527648200ac3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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