Use color images as dictionary keys, not bitmaps.

This commit is contained in:
Andrey Golovizin 2014-08-21 23:11:31 +02:00
parent c32415d843
commit 48b179e2e0
2 changed files with 10 additions and 11 deletions

View file

@ -164,18 +164,17 @@ class Image(object):
def isspace(self): def isspace(self):
return not _is_nonblank(self.bitmap) return not _is_nonblank(self.bitmap)
def tostring(self): def serialize(self):
"""Serialize the image as a string.""" """Serialize the image as some hashable object."""
height, width, *_ = self.shape bitmap = self.data.astype(np.uint8).tostring()
shape = '{}x{}'.format(height, width).encode('latin1') return self.shape, bitmap
bitmap = np.packbits(self.bitmap).tostring()
return shape + b':' + bitmap
@classmethod @classmethod
def fromstring(cls, data): def deserialize(cls, obj):
"""Deserialize an image from a string.""" """Deserialize an image."""
# TODO shape, data = obj
raise NotImplementedError array = np.fromstring(data, dtype=np.uint8).reshape(shape)
return cls(array)
def unframe(self, width=2): def unframe(self, width=2):
return Image(self.data[width:-width,width:-width]) return Image(self.data[width:-width,width:-width])

View file

@ -187,7 +187,7 @@ class Glyph(PageObject):
@property @property
def key(self): def key(self):
"""Return a dictionary key uniquely representing this glyph.""" """Return a dictionary key uniquely representing this glyph."""
return self.elevation, self.image.tostring() return self.elevation, self.image.serialize()
def is_body(self): def is_body(self):
"""Return True if the glyph is definitely not diacritic.""" """Return True if the glyph is definitely not diacritic."""