A University Project
Huffman Image Compression & LSB Steganography
2023


Ingredients
- Python
- NumPy
- Pillow
Subjects
- Algorithms
- Huffman Coding
- Steganography
About
Written for the Algorithm Design course at Shiraz University (Fall 2022), with an accompanying article co-written with Pardis Basiri. Two small Python scripts: one compresses a file losslessly with Huffman coding and decompresses it back byte for byte, the other hides a text message in the least significant bits of pixels chosen by a secret seed.
How it works
- Compression — the image is read as bytes and a frequency table is built. A min-heap merges the two least frequent nodes until one tree remains; walking the tree assigns shorter codes to more frequent bytes. The first byte of the output records the padding bits, so decompression is exact.
- Steganography — the message bits are preceded by a 32-bit length header. The seed initialises a random generator that shuffles all pixel indices, and each bit replaces the LSB of the red channel of the next pixel in that order. Saved as PNG so the bits survive. The receiver repeats the shuffle with the same seed; a wrong seed reports no message found instead of garbage.
Results
| Item | Value |
|---|---|
Original jellyfish.bmp (800×450) | 1,080,054 bytes |
| Compressed | 457,734 bytes |
| Compression ratio | 2.36 |
| Decompressed file | identical to original |
| Hidden 24-char message | 224 bits, 99 of 360,000 pixels changed by at most 1 |