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):
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])

View file

@ -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."""