Created
November 11, 2023 18:57
-
-
Save smkplus/c8e1c6b78f3384d7986bc95844e055f8 to your computer and use it in GitHub Desktop.
RandomImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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