Skip to main content

Lossless image compression using LZ77 + Huffman coding, with an optional C extension.

Project description

dipcompress

Lossless image compression built from scratch — LZ77 + Huffman coding, PNG-style row filters, and a custom .dip binary format. Ships with an optional C extension that makes LZ77 significantly faster, with a pure-Python fallback so it works everywhere.

512x512 PNG  →  786,432 B  →  301,204 B  (-61.7%)

Installation

pip install dipcompress

Requires Python 3.11+. NumPy and Pillow are pulled in automatically.

The C extension is included in the pre-built wheels (Linux x86_64, Windows AMD64, macOS x86_64 + arm64). If you install from source, it compiles automatically via setup.py. If compilation fails, the package still works — it just uses the pure-Python LZ77.


CLI

dipcompress <command> [options]

compress

dipcompress compress photo.png photo.dip
dipcompress c photo.png photo.dip          # short alias

dipcompress compress photo.png photo.dip -v   # verbose — shows each pipeline stage

Output (non-verbose):

512x512  786,432 B  ->  301,204 B  (-61.7%)

decompress

dipcompress decompress photo.dip restored.png
dipcompress d photo.dip restored.png       # short alias

dipcompress decompress photo.dip restored.png -v

info

Inspect a .dip file without decompressing it:

dipcompress info photo.dip
dipcompress i photo.dip                    # short alias

Output:

File         : photo.dip
Dimensions   : 512 x 512  (RGB)
Raw size     : 786,432 bytes  (768.0 KB)
Stored size  : 301,204 bytes  (294.1 KB)
Ratio        : 0.383  (61.7% smaller)
Row filters  : {'Paeth': 301, 'Sub': 124, 'Up': 75, 'None': 12}
Format ver   : 1

version

dipcompress --version

Python API

from dipcompress import compress, decompress

# Returns a dict with compression stats
stats = compress("photo.png", "photo.dip")
print(stats["savings_pct"])   # e.g. 61.7

decompress("photo.dip", "restored.png")

compress(input_path, output_path, verbose=False) -> dict

Key Type Description
original_size int Raw pixel bytes
compressed_size int Final .dip file size
ratio float compressed / original
savings_pct float Positive = smaller, negative = larger
width int Image width in pixels
height int Image height in pixels
channels int 1 (grayscale), 3 (RGB), 4 (RGBA)
filter_stats dict Count of each row filter type used

decompress(input_path, output_path, verbose=False)

Reconstructs the original pixel data exactly. Output can be any PIL-supported format (.png, .bmp, etc.).

Lower-level utilities

from dipcompress import load_image, save_image, pixels_to_bytes, bytes_to_pixels
from dipcompress import rle_encode, rle_decode

Checking if the C extension loaded

from dipcompress.lz77_fast import using_c
print("C extension active:", using_c)

How it works

Each image goes through a four-stage pipeline:

Image → Row Filters → LZ77 → Huffman → .dip file

1. Row filters — each row is encoded as the delta from a predictor (same idea as PNG). Five filter types are tried per row and the one that produces the smallest byte range wins: None, Sub, Up, Average, Paeth.

2. LZ77 — a sliding-window compressor that replaces repeated byte sequences with (distance, length, literal) tokens. The C extension handles this step when available; otherwise the pure-Python implementation is used.

3. Huffman coding — builds a frequency-based prefix code over the LZ77 byte stream and stores the code table in the file header.

4. .dip format — a custom binary container:

[4B magic: DIP\x01] [2B version] [4B width] [4B height]
[1B channels] [1B compression mode] [4B compressed length]
[height × 1B filter table]
[Huffman table]
[compressed payload]

Magic bytes: 44 49 50 01


Building from source

git clone https://github.com/16bitOni/dipcompress
cd dipcompress
pip install -e ".[dev]"

To explicitly build (or rebuild) the C extension:

python dipcompress/_c_ext/build.py

Run the tests:

pytest

Platform support

Platform Arch Pre-built wheel
Linux x86_64 yes
Windows AMD64 yes
macOS x86_64 yes
macOS arm64 (Apple Silicon) yes
Any source install yes (C ext compiles if toolchain available)

Python 3.11 and 3.12 are tested in CI.


Project structure

dipcompress/
├── __init__.py       # public API surface
├── cli.py            # argparse CLI
├── encoder.py        # compress() — full pipeline
├── decoder.py        # decompress() — full pipeline
├── filters.py        # PNG-style row filters
├── lz77.py           # pure-Python LZ77
├── lz77_fast.py      # C ext wrapper with Python fallback
├── huffman.py        # Huffman encode/decode
├── format.py         # .dip binary format (header, tables)
├── image_io.py       # PIL image load/save helpers
└── _c_ext/           # C source for LZ77

License

MIT — see LICENSE.


Credits

Built by Subhadip Mondal.

The compression algorithms, binary format, filter logic, and architecture were all designed and written by hand.

The GitHub Actions CI/CD pipeline, setup.py C extension build config, and PyPI publishing workflow were put together at the last minute with help from Claude — because apparently even people who implement Huffman coding from scratch draw the line at YAML indentation errors at 2 AM. No shame.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dipcompress-0.1.1.tar.gz (18.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dipcompress-0.1.1-cp312-cp312-win_amd64.whl (21.5 kB view details)

Uploaded CPython 3.12Windows x86-64

dipcompress-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (25.7 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

dipcompress-0.1.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

dipcompress-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (18.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

dipcompress-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl (18.5 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

dipcompress-0.1.1-cp311-cp311-win_amd64.whl (21.5 kB view details)

Uploaded CPython 3.11Windows x86-64

dipcompress-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (25.6 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

dipcompress-0.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

dipcompress-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (18.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

dipcompress-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl (18.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

Details for the file dipcompress-0.1.1.tar.gz.

File metadata

  • Download URL: dipcompress-0.1.1.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dipcompress-0.1.1.tar.gz
Algorithm Hash digest
SHA256 293341cbf52a16e77724bcc0c17ffafc25ff6b47ab1c7de05484d3fb3df6d9a0
MD5 684bf1d156c3ffb8ed91f913db697fdf
BLAKE2b-256 ae41db56958609756ac2f2f326104f9fd97bb295fb5a4969c870dbe62954f946

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1.tar.gz:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: dipcompress-0.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dipcompress-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 74d942ef7fbbb1f5c8a4e65d29b605de8fa25094b7de2a0627ce395fc4f72f01
MD5 d40b5fa39e17ff84e70739e511a0fc9c
BLAKE2b-256 f7e528bb0b767bf1c72aef793c4dbff28355fc61d9c70ab2c5799365cb683179

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp312-cp312-win_amd64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 79b5fd323ee38a9e7bddc2d9ca9fc59338ad44d8d67935e77500efe3a84e5782
MD5 2459f06e395e976690e2fd14edec3d2c
BLAKE2b-256 3f1ca693ee8d6369346431a5f1dbfde6076855870e248d23d4ae73fa373308b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 414cbf6389ae2dd45272a0054c93c5c225aa2e8dcec11e6903afff61ccbd8be5
MD5 00b8142babb987f9eb06fbf67eedd11a
BLAKE2b-256 10632a15afaaa39c7fddf8158b81a9ffdf8882c2c4df7839608f165da0056dbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aafcb8718cd049e711b60fcddecceb35b63fe70a546d02d2cc50fb51b612e979
MD5 f150909dc7191746fb4ff01e6baf23ed
BLAKE2b-256 88ef40e51ca956f169ae061b747b223b8f723055113d002a33eb809a03c787fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 40e5beb5cfdb9dc6719791f49cfad8306d1784b94751281fe209ef2e0f627d37
MD5 f12943bbd8aafcdcfeff6fc4d4e7213d
BLAKE2b-256 76c218be8ac7a52de7e7f628712979aa5479044f7a50153e64f7606bc3a3e50c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: dipcompress-0.1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dipcompress-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e5f12b2b6dbdb72666f0166123ed1e81b620344901cdfc8dc24b5c9419abdddd
MD5 495bf2d8c5e2c6c083f2ce74fa687c2a
BLAKE2b-256 93f082f194a663d59561309bdebec08927e9598ec43d69a3d59afa153f2374a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp311-cp311-win_amd64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 40bede3ec72e8c97aba0aa1dbedb1f3109a9925d124da07d1c25feec30f53fa7
MD5 306e4489bab6fae12e325fe9c4f2cca2
BLAKE2b-256 dd35af0b9ba21a5b06d196dc291797f8086819e0f46d686e6d410befead00b20

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 638bcb7e287f7ebdb1f9b01fed8b4fdb50749f9983404b9c92effd6f24fadddf
MD5 d3c3c02cc20ca3312c7def750a87bd53
BLAKE2b-256 119b2fa584bab508b9b7469cd6c4b34c9fb76db42cbea5fc4908ec9535ea43c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dfaa8962cd8fbc1e318159ad46096e87008979af67a4c706b6a67bbd4e9c8f88
MD5 0603e890a32dbd7d9c4fab9e0490b215
BLAKE2b-256 839175d49c05dc9d8505c1700d197e85e749e5d2e71493213d37e8040c0c011c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dipcompress-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for dipcompress-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d4c94deafc13fb3048995da72cc792cea7a388d438bcb838d897e721c533b324
MD5 f29a672be943d4ea7f8031dd93352dd7
BLAKE2b-256 cf28431d7152f6b22b9bc8be688c1be741fcd4f617b6917bb579800fab6b2300

See more details on using hashes here.

Provenance

The following attestation bundles were made for dipcompress-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: build_wheels.yml on 16bitOni/dipcompress

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page