import io import requests; import pathlib; from PIL import Image # =================================================================== # def download_image(url): filename = url.split('/')[-1]; print(f"Filename : {filename}") if(not pathlib.Path(filename).is_file()): print(f"Downloading image since '{filename}' is unable to find locally...") # with open('kitten.jpg', 'wb') as handler: handler.write(requests.get(url).content) with open(filename, 'wb') as handle: response = requests.get(url, stream=True) if not response.ok: print(response) for block in response.iter_content(1024): if not block: break handle.write(block) del response # =================================================================== # def download_N_return_image(url): filename = url.split('/')[-1]; if(not pathlib.Path(filename).is_file()): download_image(url); if(not pathlib.Path(filename).is_file()): print(f"Found '{filename}' locally"); return; else: print(f"Filename : {filename}"); return Image.open(filename); # =================================================================== # img = download_N_return_image('https://coderslegacy.com/wp-content/uploads/2020/12/kitten.jpg'); img