-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_txt.py
More file actions
executable file
·82 lines (72 loc) · 2.54 KB
/
Copy pathgen_txt.py
File metadata and controls
executable file
·82 lines (72 loc) · 2.54 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
#!/usr/bin/python3 -B
import sys, getopt
from PIL import Image
import numpy as np
from parse_template import parse_template
def get_txt(row_start, col_start, row_win, col_win, ctempl_dict, fin, char_color):
# read template and pre-process
img_rgb = Image.open(fin)
img_bin = img_rgb.convert('L')
if char_color == "Black":
img_arr = np.array(img_bin) < 180
else:
img_arr = np.array(img_bin) > 40
#Image.fromarray(img_arr).show()
txt = []
min_norm= 99999
for row in range(row_start, img_arr.shape[0], row_win):
txt.append("")
for col in range(col_start, img_arr.shape[1], col_win):
min_norm = 99999
key_selected = ''
for key, val in ctempl_dict.items():
if img_arr[row : row + row_win, col : col + col_win].shape != val.shape:
break
else:
if np.linalg.norm(img_arr[row : row + row_win, col : col + col_win] ^ val) <= min_norm:
min_norm = np.linalg.norm(img_arr[row : row + row_win, col : col + col_win] ^ val)
key_selected = key
if min_norm > 10:
txt[-1] += ' '
else:
txt[-1] += key_selected
txt[-1] = txt[-1].rstrip()
print(txt[-1])
return txt
def print_usage():
print('./gen_txt.py -i <file> -t <ftempl -c Write/Black')
def main(argv):
char_color = "Black"
fout = ""
# Get Arguments
try:
opts, args = getopt.getopt(argv,"i:t:c:o:")
except getopt.GetoptError:
print_usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_usage()
sys.exit()
elif opt in ("-i"):
fin = arg
elif opt in ("-o"):
fout = arg
elif opt in ("-t"):
ftempl = arg
elif opt in ("-c"):
char_color = arg
if fout == "":
fout = fin.split('.')[0] + '.txt`'
row_start, col_start, row_win, col_win, ctempl_dict = parse_template(ftempl, char_color)
txt = get_txt(row_start, col_start, row_win, col_win, ctempl_dict, fin, char_color)
with open(fout, 'w') as fp:
for line in txt:
print(line, file = fp)
print("\n==========================")
print("row start= %d" % row_start)
print("col start= %d" % col_start)
print("row win = %d" % row_win)
print("col win = %d" % col_win)
if __name__ == '__main__':
main(sys.argv[1:])