Skip to content

Instantly share code, notes, and snippets.

@princeppy
Last active January 20, 2023 14:27
Show Gist options
  • Save princeppy/44fcb7d04a55db4bfe899d6fa66f1d0b to your computer and use it in GitHub Desktop.
Save princeppy/44fcb7d04a55db4bfe899d6fa66f1d0b to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment