From 5f7e6abcb918064491c603bbeb16f2cea083abda Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Fri, 15 Aug 2014 14:21:20 +0200 Subject: [PATCH] Add Image.bbox(), Image.overlaps() and Image.combine(). --- pixelocr/image.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pixelocr/image.py b/pixelocr/image.py index 93bbd75..ac9ee2c 100644 --- a/pixelocr/image.py +++ b/pixelocr/image.py @@ -167,6 +167,34 @@ class Image(object): data = np.ma.masked_array(self.data, mask3, fill_value=255).filled() return Image(data, self.x, self.y) + def bbox(self, other): + return ( + min(self.left, other.left), + min(self.top, other.top), + max(self.right, other.right), + max(self.bottom, other.bottom), + ) + + def overlaps(self, other): + return not (self.right < other.left or other.left < self.right) + + def combine(self, other): + if self.overlaps(other): + raise NotImplementedError + + left, top, right, bottom = self.bbox(other) + width = right - left + height = bottom - top + selftop = self.top - top + othertop = other.top - top + selfleft = self.left - left + otherleft = other.left - left + data = np.zeros((height, width, self.shape[2]), self.data.dtype) + data.fill(255) + data[selftop:selftop + self.height, selfleft:selfleft + self.width] = self.data + data[othertop:othertop + other.height, otherleft:otherleft + other.width] = other.data + return Image(data, left, top) + def _iter_lines(self, min_space): def iter_lines(): line_start = None