-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgntparse.py
81 lines (69 loc) · 2.25 KB
/
gntparse.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
76
77
78
79
80
81
import os
import struct
from PIL import Image
def extract_imgs(src):
"""extract image object from gnt file;\n
src: the file of gnt file
"""
res = {}
count = 0
f = open(src, 'rb')
while f.read(1) != "":
f.seek(-1, 1)
count += 1
Data = f.read(4)
try:
struct.unpack('<I', Data)
except:
break
tag_code = f.read(2)
width = struct.unpack('<H', f.read(2))[0]
height = struct.unpack('<H', f.read(2))[0]
im = Image.new('RGB', (width, height))
img_array = im.load()
for x in range(0, height):
for y in range(0, width):
pixel = struct.unpack('<B', f.read(1))[0]
img_array[y, x] = (pixel, pixel, pixel)
tag = tag_code.decode("gbk")
if tag in res:
print("more code data")
else:
res[tag] = im
f.close()
return res
def save_imgs(word, im, path):
'''save image object in file;\n
word: the chinese character;\n
im: the
'''
dirpath = os.path.join(path, word)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
for i, img in enumerate(im):
filename = str(i) + '.png'
img.save(os.path.join(dirpath, str(i) + ".png"))
if __name__ == "__main__":
path = "D:\\HWDB"
trainpath = "D:\\validation"
testpath = "D:\\test"
for i in range(241, 301):
fname = os.path.join(path, "1%s-c.gnt" % (str(i).zfill(3)))
sample = extract_imgs(fname)
print("%s finished" % fname)
for k in sample:
dirpath = os.path.join(trainpath, k)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
filename = str(i) + '.png'
sample[k].save(os.path.join(dirpath, str(i) + ".png"))
# for i in range(193, 241):
# fname = os.path.join(path, "1%s-c.gnt" % (str(i).zfill(3)))
# sample = extract_imgs(fname)
# print("%s finished" % fname)
# for k in sample:
# dirpath = os.path.join(testpath, k)
# if not os.path.exists(dirpath):
# os.makedirs(dirpath)
# filename = str(i) + '.png'
# sample[k].save(os.path.join(dirpath, str(i) + ".png"))