
428 A Computational Introduction to Digital Image Processing, Second Edition
Python
# Build the dictionary.
dictionary = [chr(i) for i in range(65,91)]
buffer = dictionary[code.pop(0)]
result = buffer
for n in code:
if
n <
len(dictionary):
entry = dictionary[n]
elif n == len(dictionary):
entry = buffer + buffer[0]
else:
raise
ValueError(’Bad
compressed code: %s’ % n)
result += entry
# Add buffer+entry[0] to the dictionary.
print buffer+entry[0],len(dictionary)
dictionary += [buffer + entry[0]]
buffer = entry
return result
def imLZW(im):
"""Compress an uppercase string to a list of output code values."""
data = [format(i,’02x’) for i in im.flatten().tolist()]
# Build ...