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)
This code snippet provides a robust solution for downloading images from the internet and saving them locally:
requests for making HTTP requests (install with pip install requests)pathlib.Path for handling file paths in a cross-platform waydownload_image function takes a URL and local path as inputpip install requestsdownload_image functionThis snippet is particularly useful for web scraping projects, data collection pipelines, or any application that needs to download and store images programmatically.