-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
75 lines (53 loc) · 1.89 KB
/
utils.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import glob
from PIL import Image
from tqdm import tqdm
from io import BytesIO
from pathlib import Path
from random import choice
from base64 import b64encode
from string import ascii_lowercase, digits
thumbnail_maxsize = (200, 200)
def generate_session_password():
return ''.join([choice(ascii_lowercase + digits) for i in range(15)])
def get_bytes(image, suffix):
with BytesIO() as output:
image.save(output, suffix)
return output.getvalue()
def open_image(image_path, resize=True):
is_png = Path(image_path).suffix == '.png'
try:
if is_png:
image = Image.open(image_path).convert('RGBA')
else:
image = Image.open(image_path).convert('RGB')
size = image.size
if resize:
image.thumbnail(thumbnail_maxsize, Image.ANTIALIAS)
except Exception as e:
print(e)
return None, None
suffix = 'png' if is_png else 'jpeg'
image_bytes = get_bytes(image, suffix)
return image_bytes, size
def get_images_data(directory, files_list):
files = []
for name in tqdm(files_list):
path = os.path.join(directory, name)
image_bytes, image_size = open_image(path)
if not image_bytes:
continue
image_source = f"data:image/png;base64,{b64encode(image_bytes).decode('utf-8')}"
files.append([path, image_source, image_size])
return files
def get_files_list(directory_path):
return [i for i in sorted(os.listdir(
directory_path)) if os.path.isfile(os.path.join(directory_path, i))]
def get_directories_list(path):
return sorted([os.path.join(path, i) for i in os.listdir(path) if os.path.isdir(i)])
def path_completer(text):
if text == '~' or text == '/':
text = os.path.expanduser('~')
if os.path.isdir(text):
text += '/'
return sorted([x for x in glob.glob(text + '*/') if os.path.isdir(x)])