From 2d0ce37c791ca14987a7ef6b73d468e6352cd788 Mon Sep 17 00:00:00 2001 From: maxnth Date: Tue, 21 Nov 2023 10:29:46 +0100 Subject: [PATCH] refactor/line2page --- pagetools/cli/transformations/line2page.py | 103 +++++++++++++++++---- pagetools/src/line2page/Line2Page.py | 6 +- 2 files changed, 88 insertions(+), 21 deletions(-) diff --git a/pagetools/cli/transformations/line2page.py b/pagetools/cli/transformations/line2page.py index e21ebae..e736620 100644 --- a/pagetools/cli/transformations/line2page.py +++ b/pagetools/cli/transformations/line2page.py @@ -10,28 +10,91 @@ import click -@click.command("line2page", help="Merges line images and line texts into combined images and XML files") -@click.option('-c', '--creator', type=str, default='PAGETools', help='Creator tag for PAGE XML', show_default=True) -@click.option('-s', '--source-folder', type=str, required=True, help='Path to images and GT') -@click.option('-i', '--image-folder', default='', type=str, help='Path to images', show_default=True) -@click.option('-gt', '--gt-folder', default='', type=str, help='Path to GT', show_default=True) -@click.option('-d', '--dest-folder', default=Path(Path.cwd(), 'merged'), type=str, help='Path where output gets stored', +@click.command("line2page", + help="Merges line images and line texts into combined images and XML files") +@click.option("-c", + "--creator", + type=str, + default="PAGETools", + help="Creator tag for PAGE XML", show_default=True) -@click.option('-e', '--ext', default='.bin.png', type=str, help='Image extension', show_default=True) -@click.option('-p', '--pred', default=False, is_flag=True, help='Sets flag to also include .pred.txt', show_default=True) -@click.option('-l', '--lines', default=20, type=click.IntRange(min=0, clamp=True), help='Lines per page', +@click.option("-s", + "--source-folder", + type=str, + required=True, + help="Path to images and GT") +@click.option("-i", + "--image-folder", + type=str, + help="Path to images", show_default=True) -@click.option('-ls', '--line-spacing', default=5, type=click.IntRange(min=0, clamp=True), - help='Spacing between lines (in pixel)', show_default=True) -@click.option('-b', '--border', nargs=4, default=(10, 10, 10, 10), type=click.IntRange(min=0, clamp=True), - help='Border (in pixel): TOP BOTTOM LEFT RIGHT', show_default=True) -@click.option('--debug', default='20', type=click.Choice(['10', '20', '30', '40', '50']), - help='Sets the level of feedback to receive: DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50', +@click.option("-gt", + "--gt-folder", + type=str, + help="Path to GT", show_default=True) -@click.option('--threads', default=16, type=click.IntRange(min=1, clamp=True), help='Thread count to be used', +@click.option("-d", + "--dest-folder", + default=Path(Path.cwd(), "merged"), + type=str, + help="Path where output gets stored", + show_default=True) +@click.option("-e", + "--ext", + default=".bin.png", + type=str, + help="Image extension", + show_default=True) +@click.option("-p", + "--pred", + default=False, + is_flag=True, + help="Sets flag to also include .pred.txt", + show_default=True) +@click.option("-l", + "--lines", + default=20, + type=click.IntRange(min=0, clamp=True), + help="Lines per page", + show_default=True) +@click.option("-ls", + "--line-spacing", + default=5, + type=click.IntRange(min=0, clamp=True), + help="Spacing between lines (in pixel)", + show_default=True) +@click.option("-oe", + "--output-extension", + type=str, + help="Output image extension") +@click.option("-b", + "--border", + nargs=4, + default=(10, 10, 10, 10), + type=click.IntRange(min=0, clamp=True), + help="Border (in pixel): TOP BOTTOM LEFT RIGHT", show_default=True) +@click.option("-bg", + "--background-color", + nargs=3, + default=(255, 255, 255), + type=click.IntRange(min=0, max=255, clamp=True), + help="RGB background color", + show_default=True) +@click.option("--debug", + default="20", + type=click.Choice(["10", "20", "30", "40", "50"]), + help="Sets the level of feedback to receive: DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50", + show_default=True) +@click.option("--threads", + default=16, + type=click.IntRange(min=1, clamp=True), + help="Thread count to be used", + show_default=True) +@click.option("--xml-schema", + default="2019", + type=click.Choice(["2017", "2019"]), + help="Sets the year of the xml-Schema to be used", show_default=True) -@click.option('--xml-schema', default='2019', type=click.Choice(['2017', '2019']), - help='Sets the year of the xml-Schema to be used', show_default=True) def line2page_cli(creator: str, source_folder: str, image_folder: str, @@ -41,7 +104,9 @@ def line2page_cli(creator: str, pred: str, lines: int, line_spacing: int, + output_extension: str, border: Tuple[int], + background_color: Tuple[int], debug: bool, threads: int, xml_schema: str): @@ -53,7 +118,7 @@ def line2page_cli(creator: str, tic = time.perf_counter() opt_obj = Line2Page(creator, source_folder, image_path, gt_path, dest_folder, ext, pred, lines, line_spacing, - border, debug, threads, xml_schema) + output_extension, border, background_color, debug, threads, xml_schema) opt_obj.match_files() pages = list(opt_obj.chunks(opt_obj.matches, opt_obj.lines)) diff --git a/pagetools/src/line2page/Line2Page.py b/pagetools/src/line2page/Line2Page.py index 0de3b96..3013620 100644 --- a/pagetools/src/line2page/Line2Page.py +++ b/pagetools/src/line2page/Line2Page.py @@ -24,7 +24,9 @@ def __init__(self, pred: str, lines: int, spacing: int, + output_extension: str, border: Tuple[int], + background_color: Tuple[int], debug: bool, threads: int, xml_schema: str): @@ -58,9 +60,9 @@ def __init__(self, # Extension strings used self.gt_suffix = ".gt.txt" self.pred_suffix = ".pred.txt" - self.img_suffix = '.nrm.png' + self.img_suffix = output_extension if output_extension else ext - self.background_colour = (255, 255, 255) + self.background_colour = background_color self.colour_channels = 3 if border[1] > lines: footer_size = border[1] - lines