From 48b179e2e0a97b3958cbfe0cc0662fb40dc9092b Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Thu, 21 Aug 2014 23:11:31 +0200 Subject: [PATCH] Use color images as dictionary keys, not bitmaps. --- pixelocr/image.py | 19 +++++++++---------- pixelocr/page.py | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pixelocr/image.py b/pixelocr/image.py index 4e24ff2..1d6da27 100644 --- a/pixelocr/image.py +++ b/pixelocr/image.py @@ -164,18 +164,17 @@ class Image(object): def isspace(self): return not _is_nonblank(self.bitmap) - def tostring(self): - """Serialize the image as a string.""" - height, width, *_ = self.shape - shape = '{}x{}'.format(height, width).encode('latin1') - bitmap = np.packbits(self.bitmap).tostring() - return shape + b':' + bitmap + def serialize(self): + """Serialize the image as some hashable object.""" + bitmap = self.data.astype(np.uint8).tostring() + return self.shape, bitmap @classmethod - def fromstring(cls, data): - """Deserialize an image from a string.""" - # TODO - raise NotImplementedError + def deserialize(cls, obj): + """Deserialize an image.""" + shape, data = obj + array = np.fromstring(data, dtype=np.uint8).reshape(shape) + return cls(array) def unframe(self, width=2): return Image(self.data[width:-width,width:-width]) diff --git a/pixelocr/page.py b/pixelocr/page.py index aa4feb5..98616e7 100644 --- a/pixelocr/page.py +++ b/pixelocr/page.py @@ -187,7 +187,7 @@ class Glyph(PageObject): @property def key(self): """Return a dictionary key uniquely representing this glyph.""" - return self.elevation, self.image.tostring() + return self.elevation, self.image.serialize() def is_body(self): """Return True if the glyph is definitely not diacritic."""