From a44091b5705696f39a43d7099c885bc876c67318 Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Mon, 1 Sep 2014 14:53:59 +0200 Subject: [PATCH] Add Image.color and PageObject.color. --- pixelocr/image.py | 11 +++++++++++ pixelocr/page.py | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/pixelocr/image.py b/pixelocr/image.py index 2ba94f2..7f57984 100644 --- a/pixelocr/image.py +++ b/pixelocr/image.py @@ -18,6 +18,7 @@ import itertools from io import BytesIO import numpy as np +from scipy.stats.mstats import mode from skimage.io import imread, imsave from .utils import cached_property, pairwise @@ -162,6 +163,16 @@ class Image(object): def isspace(self): return not is_nonblank(self.bitmap) + @cached_property + def color(self): + """Return the most frequent foreground color.""" + if self.isspace: + return None + mask3 = np.dstack([~self.bitmap] * 3) + colors = np.ma.MaskedArray(self.data, mask3).reshape(-1, 3) + modes, counts = mode(colors) + return tuple(modes[0]) + def serialize(self): """Serialize the image as some hashable object.""" bitmap = self.data.astype(np.uint8).tostring() diff --git a/pixelocr/page.py b/pixelocr/page.py index cc0e384..910cfa1 100644 --- a/pixelocr/page.py +++ b/pixelocr/page.py @@ -80,6 +80,10 @@ class PageObject(object): def ycenter(self): return (self.bottom - self.top) / 2 + @property + def color(self): + return self.image.color + def fits(self, left, top, right, bottom): """Return True if the glyph fits into the given bounding box."""