diff --git a/pixelocr/image.py b/pixelocr/image.py index 3419556..d115486 100644 --- a/pixelocr/image.py +++ b/pixelocr/image.py @@ -45,15 +45,32 @@ class Image(object): @cached_property def bitmap(self): + """Return a two-color version of the image. + + 0 = white (blank) pixel + 1 = black (letter) pixel + """ + grayscale = rgb2gray(self._data) return (grayscale < 1).astype('b') @property def key(self): + """Return a byte string uniquely representing the image.""" + return self.tostring() + + def tostring(self): + """Serialize the image as a string.""" height, width, *_ = self.shape - shape = '{}x{}'.format(height, width) + shape = '{}x{}'.format(height, width).encode('latin1') bitmap = np.packbits(self.bitmap).tostring() - return shape.encode('latin1') + b':' + bitmap + return shape + b':' + bitmap + + @classmethod + def fromstring(cls, data): + """Deserialize an image from a string.""" + # TODO + raise NotImplementedError def unframe(self, width=2): return self[width:-width,width:-width]