| import requests | |
| from PIL import Image | |
| import urllib.parse as parse | |
| import os | |
| DEFAULT_URL = "https://images.unsplash.com/photo-1532635236-d50c8592eb08?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1411&q=80" | |
| # Verify url | |
| def check_url(string): | |
| try: | |
| result = parse.urlparse(string) | |
| return all([result.scheme, result.netloc, result.path]) | |
| except: | |
| return False | |
| # Load an image | |
| def load_image(image_path=None): | |
| if image_path and check_url(image_path): | |
| return Image.open(requests.get(image_path, stream=True).raw) | |
| elif os.path.exists(image_path): | |
| return Image.open(image_path.read()) | |
| else: | |
| return Image.open(requests.get(DEFAULT_URL, stream=True).raw) | |