-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
39 lines (33 loc) · 1.13 KB
/
data.py
File metadata and controls
39 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Python Challenge Data Handler.
Use requests to fetch external data and save it into a specified file path.
"""
from PIL import Image
from StringIO import StringIO
import requests, os
def open_image(image_file, image_url, auth=None):
"""
Attempt to create an Image object by opening the provided
image_file. Returns the Image object. If the file does not
exist, the image will be fetched from the image_url.
"""
try:
return Image.open(image_file)
# Request image and save it if not exist
except IOError:
save_image(image_url, image_file, auth)
return Image.open(image_file)
def save_image(url, filename, auth=None):
"""
Uses requests library to retrieve image from url.
Parses the request content to an Image object and
saves the image into the images directory
"""
# Request image content from url
print("Downloading image: " + url)
image_req = requests.get(url, auth=auth).content
image = Image.open(StringIO(image_req))
# Save image into image directory
if not os.path.exists('images'):
os.makedirs('images')
image.save(filename)