-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
70 lines (57 loc) · 1.65 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
import toml
import os
import uuid
from PIL import Image
from sys import version_info
# helper function
def get_conf(conf_file):
""" configuration file in toml format """
with open(conf_file, 'r') as conf_h:
conf = toml.loads(conf_h.read())
return conf
# helper function
def read_delete(path):
"""
:param path: out.txt path
:return: list data splited
"""
with open(path, 'r') as f:
data = f.read()
os.remove(path)
return data.split('\n')
# helper class
class CustomImage(object):
def __init__(self, size, data, name):
self.size = size
self.data = data
self.name = name
def custome_save(self, num):
# TODO: will be closed soon.
path_str = 'debug/{}'.format(num)
if version_info[0] == 2:
try:
os.makedirs(path_str, )
except:
pass
else:
os.makedirs(path_str, exist_ok=True)
new_ = Image.new('RGB', self.size)
new_.putdata(self.data, scale=1.0, offset=0.0)
if self.name == 'good':
new_.convert('L').save(path_str + '/{}.png'.format(self.name))
else:
new_.save(path_str + '/{}.png'.format(self.name))
class CustomText(object):
def __init__(self, raw_str):
self.str = raw_str
def custome_save(self, num):
path_str = 'debug/{}'.format(num)
if version_info[0] == 2:
try:
os.makedirs(path_str, )
except:
pass
else:
os.makedirs(path_str, exist_ok=True)
with open(path_str + '/out.txt', 'w') as f:
f.write(self.str)