forked from ankush-me/SynthText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractImage.py
35 lines (29 loc) · 1.01 KB
/
extractImage.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
# -*- coding: utf-8 -*-
import h5py as hp
from PIL import Image, ImageDraw
import os
def DrawBoxes(draw, bb, color):
for i in range(0, bb.shape[2]):
box = bb[:, :, i]
path = []
for j in range(0, box.shape[1]):
pos = box[:, j]
path.append(pos[0])
path.append(pos[1])
path.append(box[0, 0])
path.append(box[1, 0])
draw.line(path, color, 1)
def ExtractFiles(h5path, output_dir):
file = hp.File(h5path, 'r')
data = file['data']
for key in data.keys():
image = data[key]
charbb = image.attrs['charBB']
wordbb = image.attrs['wordBB']
image = Image.fromarray(image[:])
draw = ImageDraw.Draw(image)
DrawBoxes(draw, charbb, 'red')
DrawBoxes(draw, wordbb, 'blue')
image.save(os.path.join(output_dir, key+ '.jpg'))
if __name__ == '__main__':
ExtractFiles('./results/SynthText.h5', './results/viz')