Skip to content

Instantly share code, notes, and snippets.

@zealinux
Forked from mjdietzx/download_image.py
Created April 18, 2019 15:06
Show Gist options
  • Save zealinux/fc476468d97ffabe79fd4bdf088477ad to your computer and use it in GitHub Desktop.
Save zealinux/fc476468d97ffabe79fd4bdf088477ad to your computer and use it in GitHub Desktop.
Download image from url and save as file
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