#!/usr/bin/env python
"""
Reads in a bunch of ALTO documents and repolygonizes the lines contained with
the kraken polygonizer.
"""

import click

@click.command()
@click.option('-f', '--format-type', type=click.Choice(['alto', 'page', 'xml']), default='xml',
              help='Sets the input document format. In ALTO and PageXML mode all'
              'data is extracted from xml files containing both baselines, polygons, and a'
              'link to source images.')
@click.argument('files', nargs=-1)
def cli(format_type, files):
    """
    A small script repolygonizing line boundaries in ALTO or PageXML files.
    """
    if len(files) == 0:
        ctx = click.get_current_context()
        click.echo(ctx.get_help())
        ctx.exit()

    import os
    import numpy as np
    import sys
    from lxml import etree
    from os.path import splitext

    from kraken.lib import xml
    from kraken import serialization, rpred
    from kraken.lib.dataset import _fixed_resize
    from PIL import Image
    from kraken.lib.segmentation import calculate_polygonal_environment, scale_polygonal_lines

    def _repl_alto(fname, polygons):
        with open(fname, 'rb') as fp:
            doc = etree.parse(fp)
            lines = doc.findall('.//{*}TextLine')
            idx = 0
            for line in lines:
                pol = line.find('./{*}Shape/{*}Polygon')
                if pol is not None:
                    pol.attrib['POINTS'] = ' '.join([str(coord) for pt in polygons[idx] for coord in pt])
                    idx += 1
            with open(splitext(fname)[0] + '_rewrite.xml', 'wb') as fp:
                doc.write(fp, encoding='UTF-8', xml_declaration=True)

    def _repl_page(fname, polygons):
        with open(fname, 'rb') as fp:
            doc = etree.parse(fp)
            lines = doc.findall('.//{*}TextLine')
            idx = 0
            for line in lines:
                pol = line.find('./{*}Shape/{*}Polygon')
                if pol is not None:
                    pol.attrib['POINTS'] = ' '.join([str(coord) for pt in o[idx] for coord in pt])
                    idx += 1
            with open(splitext(fname)[0] + '_rewrite.xml', 'wb') as fp:
                doc.write(fp, encoding='UTF-8', xml_declaration=True)

    if format_type == 'xml':
        parse_fn = xml.parse_xml
    elif format_type == 'page':
        parse_fn = xml.parse_page
    else:
        parse_fn = xml.parse_alto

    for doc in files:
        click.echo(f'Processing {doc} ', nl=False)
        seg = parse_fn(doc)
        im = Image.open(seg['image']).convert('L')
        l = []
        for x in seg['lines']:
            bl = x['baseline'] if x['baseline'] is not None else [0, 0]
            l.append(bl)
        o = calculate_polygonal_environment(im, l, scale=(1800, 0))
        print(o)

if __name__ == '__main__':
    cli()

@@ -231,15 +231,8 @@
 age'
-, 'xml'
 %5D), 
@@ -240,19 +240,20 @@
 efault='
-xml
+alto
 ',%0A     
@@ -930,57 +930,8 @@
 red%0A
-    from kraken.lib.dataset import _fixed_resize%0A
     
@@ -1877,32 +1877,22 @@
 d('./%7B*%7D
-Shape/%7B*%7DPolygon
+Coords
 ')%0A     
@@ -1946,38 +1946,38 @@
     pol.attrib%5B'
-POINTS
+points
 '%5D = ' '.join(%5Bs
@@ -1979,51 +1979,55 @@
 in(%5B
-str(coord
+','.join(%5Bstr(x
 ) for 
-pt
+x
  in 
-o%5Bidx%5D for coord in pt
+pt%5D) for pt in o%5Bidx%5D
 %5D)%0A 
@@ -2222,72 +2222,8 @@
 e ==
- 'xml':%0A        parse_fn = xml.parse_xml%0A    elif format_type ==
  'pa
@@ -2257,24 +2257,53 @@
 .parse_page%0A
+        repl_fn = _repl_page%0A
     else:%0A  
@@ -2333,16 +2333,45 @@
 rse_alto
+%0A        repl_fn = _repl_alto
 %0A%0A    fo
@@ -2739,14 +2739,21 @@
     
-print(
+repl_fn(doc, 
 o)%0A%0A

rewrite repolygonization script