Python Snippets

Download and Save an Image from a URL in Python

This snippet demonstrates how to download an image from a URL and save it locally using Python. This is useful for automating image collection, web scraping, or saving images for processing.

import requests
from pathlib import Path

def download_image(url, save_path):
    """
    Downloads an image from a URL and saves it to the specified path.
    
    Args:
        url (str): URL of the image to download
        save_path (str): Local path where the image should be saved
    """
    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()  # Raise an exception for HTTP errors
        
        # Create parent directories if they don't exist
        Path(save_path).parent.mkdir(parents=True, exist_ok=True)
        
        with open(save_path, 'wb') as file:
            for chunk in response.iter_content(1024):
                file.write(chunk)
        
        print(f"Image successfully saved to {save_path}")
    except requests.exceptions.RequestException as e:
        print(f"Error downloading image: {e}")

# Example usage:
image_url = "https://example.com/image.jpg"
local_path = "images/downloaded_image.jpg"
download_image(image_url, local_path)

Explanation

This code snippet provides a robust solution for downloading images from the internet and saving them locally:

  1. Imports:
    • requests for making HTTP requests (install with pip install requests)
    • pathlib.Path for handling file paths in a cross-platform way
  2. Functionality:
    • The download_image function takes a URL and local path as input
    • It makes a streaming HTTP GET request to the URL
    • The response is checked for errors
    • Any necessary parent directories are created automatically
    • The image is saved in chunks (memory-efficient for large files)
    • Success/error messages are printed appropriately
  3. Why it’s useful:
    • Handles errors gracefully (invalid URLs, network issues)
    • Works with large files efficiently due to streaming
    • Creates directories automatically if they don’t exist
    • Uses modern Python practices (pathlib instead of os.path)
  4. How to use:
    • Install the required package: pip install requests
    • Replace the example URL with a real image URL
    • Specify your desired save path
    • Call the download_image function

This snippet is particularly useful for web scraping projects, data collection pipelines, or any application that needs to download and store images programmatically.