-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
104 lines (77 loc) · 2.5 KB
/
util.py
File metadata and controls
104 lines (77 loc) · 2.5 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/local/bin/python
# coding: utf-8
import errno
import json
import os
import pickle
from io import BytesIO
from PIL import Image
import config
def save_cookie(driver, path):
with open(path, 'wb') as f:
pickle.dump(driver.get_cookies(), f)
def load_cookie(driver, path):
with open(path, 'rb') as f:
cookies = pickle.load(f)
for cookie in cookies:
driver.add_cookie(cookie)
def save_file(code, filename, text, mode='w'):
path = get_file_path(code, filename)
create_path(path)
with open(path, mode) as f:
log('保存文本到 %s' % path)
f.write(text)
def read_file(code, filename):
path = get_file_path(code, filename)
if not os.path.isfile(path):
return ''
with open(path, 'r') as f:
return f.read()
def load_json(code):
path = get_file_path(code, 'json')
create_path(path)
with open(path, 'r') as f:
saved_data = json.load(f)
log('从 %s 取出 %s ' % (path, str(saved_data)))
return saved_data
def save_json(code, key, value):
path = get_file_path(code, 'json')
create_path(path)
saved_data = {}
if os.path.isfile(path):
saved_data = load_json(code)
saved_data[key] = value
with open(path, 'w') as f:
log('保存 { %s : %s } 到 %s ' % (key, value, path))
json.dump(saved_data, f)
def get_img_path(code, filename):
path = os.path.join('private/' + code, filename + r".png")
return path
def get_file_path(code, filename):
path = os.path.join('private/' + code, filename + r".txt")
return path
def create_path(path):
if not os.path.exists(os.path.dirname(path)):
try:
os.makedirs(os.path.dirname(path))
except OSError as e: # Guard against race condition
if e.errno != errno.EEXIST:
raise
def get_code_type(code):
code_type = 'sh'
if code[0] == '0' or code[0] == '3':
code_type = 'sz'
return code_type
def log(content):
print('========>> %s ' % content)
def save_rect_element(png, left, top, right, bottom, code, filename):
left = config.FACTOR * left
top = config.FACTOR * top
right = config.FACTOR * right
bottom = config.FACTOR * bottom
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
im = im.crop((left, top, right, bottom)) # defines crop points
path = get_img_path(code, filename)
log('保存图片到 %s' % path)
create_path(path)
im.save(path) # saves new cropped image