Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. def persist_image(folder_path:str,url:str):
  2. try:
  3. image_content = requests.get(url).content
  4.  
  5. except Exception as e:
  6. print(f"ERROR - Could not download {url} - {e}")
  7.  
  8. try:
  9. image_file = io.BytesIO(image_content)
  10. image = Image.open(image_file).convert('RGB')
  11. file_path = os.path.join(folder_path,hashlib.sha1(image_content).hexdigest()[:10] + '.jpg')
  12. with open(file_path, 'wb') as f:
  13. image.save(f, "JPEG", quality=85)
  14. print(f"SUCCESS - saved {url} - as {file_path}")
  15. except Exception as e:
  16. print(f"ERROR - Could not save {url} - {e}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement