Here is an example Python script that downloads images from a given URL and saves them to a local directory:
import requests
def download_image(url, save_path):
# Open the image URL
response = requests.get(url)
# Open the save path
with open(save_path, 'wb') as f:
# Write the image data to the save path
f.write(response.content)
# Example usage
url = "https://images.unsplash.com/photo-1593642532857-b1ebc8e7c147"
save_path = "image.jpg"
download_image(url, save_path)
This script uses the requests
library to make a GET request to the image URL, which returns the image data. It then opens a file at the specified save path, and writes the image data to it in binary mode. You can use a loop and a list of urls to download multiple images or use a function to pass the url and save_path as arguments. You can also customize this script to handle errors, such as if the URL is not valid or the image cannot be downloaded for some reason. You can also add file extensions to the save path depending on the type of image you are trying to download.