Skip to content

Instantly share code, notes, and snippets.

@smkplus
Created November 11, 2023 18:57
Show Gist options
  • Save smkplus/c8e1c6b78f3384d7986bc95844e055f8 to your computer and use it in GitHub Desktop.
Save smkplus/c8e1c6b78f3384d7986bc95844e055f8 to your computer and use it in GitHub Desktop.
RandomImage
import requests
# Define the URL
url = "https://source.unsplash.com/random/900x700/"
# Send an HTTP GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Get the image content
image_content = response.content
# Specify a filename for the image
filename = "fruit_image.jpg"
# Save the image to a file
with open(filename, 'wb') as f:
f.write(image_content)
print(f"Image saved as {filename}")
else:
print(f"Failed to fetch the image. Status code: {response.status_code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment