Created
November 29, 2020 11:05
-
-
Save pg-goose/985e4fc8b7907ef6b7d00d8b27eea421 to your computer and use it in GitHub Desktop.
Download image by URL [python | requests | wget]
This file contains hidden or 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 | |
| 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