forked from lizard1998myx/MultiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
27 lines (21 loc) · 860 Bytes
/
utils.py
File metadata and controls
27 lines (21 loc) · 860 Bytes
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
import os, requests, datetime
from PIL import Image
from io import BytesIO
def image_filename(header='MultiBot', post='.jpg'):
return datetime.datetime.now().strftime('{}_image_%Y%m%d-%H%M%S{}'.format(header, post))
def format_filename(header='MultiBot', type='image', post='.txt'):
return datetime.datetime.now().strftime(f'{header}_{type}_%Y%m%d-%H%M%S{post}')
def image_url_to_path(url, header='MultiBot'):
resp = requests.get(url)
image = Image.open(BytesIO(resp.content))
filename = image_filename(header=header)
abs_dir = os.path.abspath(os.path.join('..', 'temp'))
abs_path = os.path.join(abs_dir, filename)
try:
os.mkdir(abs_dir)
except FileExistsError:
pass
if image.mode == "P":
image = image.convert('RGB')
image.save(abs_path)
return abs_path