Skip to content

Instantly share code, notes, and snippets.

@ChaitanyaBaweja
Last active March 9, 2023 04:28
Show Gist options
  • Save ChaitanyaBaweja/39fb3f049d2a4a2389b40acd5a1a33b6 to your computer and use it in GitHub Desktop.
Save ChaitanyaBaweja/39fb3f049d2a4a2389b40acd5a1a33b6 to your computer and use it in GitHub Desktop.
## Importing Necessary Modules
import requests # to get image from the web
import shutil # to save it locally
## Set up the image URL and filename
image_url = "https://cdn.pixabay.com/photo/2020/02/06/09/39/summer-4823612_960_720.jpg"
filename = image_url.split("/")[-1]
# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream = True)
# Check if the image was retrieved successfully
if r.status_code == 200:
# Set decode_content value to True, otherwise the downloaded image file's size will be zero.
r.raw.decode_content = True
# Open a local file with wb ( write binary ) permission.
with open(filename,'wb') as f:
shutil.copyfileobj(r.raw, f)
print('Image sucessfully Downloaded: ',filename)
else:
print('Image Couldn\'t be retreived')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment