Skip to content

Instantly share code, notes, and snippets.

@pg-goose
Created November 29, 2020 11:05
Show Gist options
  • Select an option

  • Save pg-goose/985e4fc8b7907ef6b7d00d8b27eea421 to your computer and use it in GitHub Desktop.

Select an option

Save pg-goose/985e4fc8b7907ef6b7d00d8b27eea421 to your computer and use it in GitHub Desktop.
Download image by URL [python | requests | wget]
import requests
import shutil
#Setting URL and image name
image_url = "##########URL TO DOWNLOAD"##########"
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