Can you provide Python code to download an image from a URL and convert it to a thumbnail?
- User
Can you provide Python code to download an image from a URL and convert it to a thumbnail?
- Assistant
Certainly! Here is a Python script that downloads an image from a URL, creates a thumbnail, and saves it to your local machine. This script uses the
requestslibrary for downloading the image and thePILlibrary for image processing. Make sure to install these libraries using pip before running the script:pip install requests pip install pillowHere's the code:
import requests from PIL import Image from io import BytesIO def download_image(url): response = requests.get(url) return Image.open(BytesIO(response.content)) def create_thumbnail(image, size=(128, 128)): image.thumbnail(size) return image def save_image(image, filename): image.save(filename) if __name__ == "__main__": url = "https://example.com/image.jpg" # Replace with the actual URL image = download_image(url) thumbnail = create_thumbnail(image) save_image(thumbnail, "thumbnail.jpg")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.