From d3c32b82ab4c3ea5a68d7b7c1902e3c5513aa3d5 Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Fri, 12 Sep 2014 12:35:44 +0200 Subject: [PATCH] Add Configuration() class and make it accessible as Document.config and PageObject.config. --- pixelocr/config.py | 27 +++++++++++++++++++++++++++ pixelocr/document.py | 4 +++- pixelocr/page.py | 8 +++++--- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 pixelocr/config.py diff --git a/pixelocr/config.py b/pixelocr/config.py new file mode 100644 index 0000000..5b700cc --- /dev/null +++ b/pixelocr/config.py @@ -0,0 +1,27 @@ +# Copyright (C) 2014 Andrey Golovizin +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +from confire import Configuration as BaseConfiguration + + +class Configuration(BaseConfiguration): + min_body_height = 10 + + @classmethod + def load_file(cls, filename): + class MyConfiguration(cls): + CONF_PATHS = [filename] + return MyConfiguration.load() diff --git a/pixelocr/document.py b/pixelocr/document.py index b8f6f4e..7ffaf57 100644 --- a/pixelocr/document.py +++ b/pixelocr/document.py @@ -22,6 +22,7 @@ from . import formatting from .image import Image from .page import Page, Space from .glyphdb import GlyphDB, SPACE, NEWLINE +from .config import Configuration class Document(object): @@ -33,6 +34,7 @@ class Document(object): self.ui = ui self.filenames = sorted(glob(path.join(dirname, '*.png')))[skip:skip + limit if limit else None] self.glyphdb = GlyphDB(path.join(self.dirname, 'glyphdb.pickle')) + self.config = Configuration.load_file(path.join(self.dirname, 'config.yaml')) self.output_format = output_format self.last_style = (False, False, (255, 255, 255)) # FIXME get rid of hardcoded value @@ -40,7 +42,7 @@ class Document(object): self.glyphdb.save() def load_page(self, filename): - return Page(Image.fromfile(filename), filename) + return Page(self, Image.fromfile(filename), filename) def recognize(self): for filename in self.filenames: diff --git a/pixelocr/page.py b/pixelocr/page.py index ca55904..88c75e8 100644 --- a/pixelocr/page.py +++ b/pixelocr/page.py @@ -29,9 +29,11 @@ CONNECTIVITY8 = ndimage.generate_binary_structure(2, 2) class PageObject(object): - def __init__(self, image, filename=None): + def __init__(self, document, image, filename=None): self.image = image self.filename = filename + self.document = document + self.config = document.config def _repr_png_(self): return self.image._repr_png_() @@ -163,7 +165,7 @@ class Page(PageObject): class Line(PageObject): def __init__(self, page, image): - super().__init__(image) + super().__init__(page.document, image) self.page = page def __iter__(self): @@ -247,7 +249,7 @@ class Glyph(PageObject): MIN_BODY_HEIGHT = 10 def __init__(self, line, image, elevation): - super().__init__(image) + super().__init__(line.document, image) self.elevation = elevation self.line = line