Encode and decode huffman from different scripts (Python).

asked 2018-07-11 00:40:44 +0200

AntGeorge gravatar image

I am using the PIL module to get the values of each pixel from an 2x2 RGB image.

img = ['70', '64', '28', '167', '120', '17', '75', '56', '9', '98', '98', '99']

(every 3 values = 1 pixel)

the frequency_table(img) gives

freq = {'120': 1, '167': 1, '17': 1, '28': 1, '56': 1, '64': 1, '70': 1, '75': 1, '9': 1, '98': 2, '99': 1}

I want to encode this image with Huffman (in script client.py), save the encoded string to file and then open this file from another program (script server.py) and decode it.

I learned that this is not a canonical Huffman code (from #25798 ticket i created), so i can't just use the freq to create the Huffman for the decoding.

The solution i found is to get the _tree and the _index attributes from the encode object and replace them in the decode object. So the decode object will have the same _tree and _index as the encode.

Is there any better solution?

Sorry for my english. Thanks.

edit retag flag offensive close merge delete