This snippet demonstrates how to download multiple images in parallel using Python’s concurrent.futures
and tqdm
for a progress bar. This is useful for batch downloading images from URLs while maintaining performance and visibility of progress.
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import os
def download_image(url, save_path):
"""Download an image from a URL and save it to the specified path."""
response = requests.get(url, stream=True)
response.raise_for_status()
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return save_path
def download_images_parallel(urls, output_dir="downloaded_images"):
"""Download multiple images in parallel with a progress bar."""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with ThreadPoolExecutor(max_workers=5) as executor:
futures = []
for i, url in enumerate(urls):
save_path = os.path.join(output_dir, f"image_{i + 1}.jpg")
futures.append(executor.submit(download_image, url, save_path))
for future in tqdm(as_completed(futures), total=len(futures), desc="Downloading images"):
future.result() # Raise exceptions if any
if __name__ == "__main__":
# Example usage
image_urls = [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg",
]
download_images_parallel(image_urls)
ThreadPoolExecutor
to download multiple images concurrently, improving speed.tqdm
to display a progress bar, showing the status of downloads.raise_for_status()
method ensures failed downloads (e.g., 404 errors) are caught early.pip install requests tqdm
image_urls
with your target URLs../downloaded_images/
.This snippet solves a common need (batch image downloads) while optimizing for speed and usability.