summaryrefslogtreecommitdiff
path: root/third_party/spiro/font/cut.py
blob: 541efa816a16685d11507844d5b12e44574dfeb2 (plain)
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
import sys

athresh = 100
border = 20

segf = sys.argv[1]
if len(sys.argv) > 2:
    pref = sys.argv[2]
else:
    pref = '/tmp/cut'
rects = []
starts = {}
for l in file(segf).xreadlines():
    ls = l.split()
    if len(ls) == 6 and ls[-1] == 'rect':
        r = map(int, ls[:4])
        area = (r[2] - r[0]) * (r[3] - r[1])
        if area > athresh:
            rpad = [r[0] - border, r[1] - border, r[2] + border, r[3] + border]
            if not starts.has_key(rpad[1]):
                starts[rpad[1]] = []
            starts[rpad[1]].append(len(rects))
            rects.append(rpad)
inf = sys.stdin
l = inf.readline()
if l != 'P5\n':
    raise 'expected pgm file'
while 1:
    l = inf.readline()
    if l[0] != '#': break
x, y = map(int, l.split())
l = inf.readline()

active = {}
for j in range(y):
    if starts.has_key(j):
        for ix in starts[j]:
            r = rects[ix]
            ofn = pref + '%04d.pgm' % ix
            of = file(ofn, 'w')
            active[ix] = of
            print >> of, 'P5'
            print >> of, r[2] - r[0], r[3] - r[1]
            print >> of, '255'
    buf = inf.read(x)
    for ix, of in active.items():
        r = rects[ix]
        of.write(buf[r[0]:r[2]])
        if j == r[3] - 1:
            of.close()
            del active[ix]