-
-
Save zealinux/fc476468d97ffabe79fd4bdf088477ad to your computer and use it in GitHub Desktop.
Download image from url and save as file
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 io | |
from PIL import Image # https://pillow.readthedocs.io/en/4.3.x/ | |
import requests # http://docs.python-requests.org/en/master/ | |
# example image url: https://m.media-amazon.com/images/S/aplus-media/vc/6a9569ab-cb8e-46d9-8aea-a7022e58c74a.jpg | |
def download_image(url, image_file_path): | |
r = requests.get(url, timeout=4.0) | |
if r.status_code != requests.codes.ok: | |
assert False, 'Status code error: {}.'.format(r.status_code) | |
with Image.open(io.BytesIO(r.content)) as im: | |
im.save(image_file_path) | |
print('Image downloaded from url: {} and saved to: {}.'.format(url, image_file_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment