Skip to main content

PyGFWX

A Python implementation of the GFWX (Good, Fast Wavelet Codec) for educational purposes.

Orange Tabby Cat

Test image: "Orange tabby cat sitting on fallen leaves" by Hisashi (derivative work: Caspian blue), CC BY-SA 2.0

Overview

PyGFWX is an educational implementation of the GFWX wavelet codec, created to learn and understand wavelet-based image compression. The implementation prioritizes readability over performance - it's written in pure Python with NumPy for data structures, but no optimized libraries for the core algorithms.

Note: This is a learning tool, not a production codec. For high-performance encoding, use the reference C++ SDK. PyGFWX is useful for understanding how wavelet compression works, experimenting with the algorithm, and educational purposes.

Compression Demo

Using the cat image above (768×1024 RGB), here are compression results at different quality levels:

Quality Compressed Size Compression Ratio PSNR
64 (high compression) 64 KB 36:1 37.4 dB
256 (medium) 246 KB 9.4:1 43.9 dB
512 (high quality) 483 KB 4.8:1 47.6 dB
1024 (lossless) 847 KB 2.7:1 ∞

Original uncompressed size: 2.25 MB (768×1024×3 bytes)

Features

  • Wavelet compression using lifting scheme (5/3 and 9/7 wavelets)
  • Golomb-Rice entropy coding with context-adaptive modeling
  • Progressive decoding with downsampling support
  • Multiple color modes: Mono, RGB, RGBA, Bayer patterns
  • Quality range: Lossy (1) to lossless (1024)
  • Multi-layer support: Stereo images, depth maps, animation frames
  • Custom color transforms: Programmable UYV, A710, and custom transforms
  • Metadata support: JSON, key-value, or binary metadata blocks

Why Pure Python?

This project implements the full codec in readable Python to make it easy to:

  • Step through the compression pipeline with a debugger
  • Understand each stage: wavelets, quantization, entropy coding
  • Experiment with modifications to the algorithm
  • Learn wavelet compression without deciphering optimized C++

The code is validated against the reference SDK to ensure correctness, but runs significantly slower (expect 100x+ slower than the C++ implementation).

Installation

This project uses uv for dependency management.

# Clone the repository
git clone https://github.com/chiplukes/pygfwx.git
cd pygfwx

# Install dependencies
uv sync

# Install with development dependencies
uv sync --all-extras

Quick Start

from pygfwx import encode, decode
import numpy as np

# Create or load an image (numpy array)
image = np.random.randint(0, 256, (256, 256), dtype=np.uint8)

# Encode (compress)
compressed = encode(image, quality=512)

# Decode (decompress)
decoded = decode(compressed)

# Verify
assert np.array_equal(image, decoded)  # True for lossless (quality=1024)

Project Structure

pygfwx/
├── src/pygfwx/          # Main Python package
│   ├── core/            # Core codec implementation
│   │   ├── bitstream.py     # Bit-level I/O
│   │   ├── header.py        # GFWX header parsing
│   │   ├── lifting.py       # Wavelet transforms
│   │   ├── golomb_rice.py   # Entropy coding
│   │   ├── context.py       # Context modeling
│   │   ├── encoder.py       # Coefficient encoding
│   │   ├── decoder.py       # Coefficient decoding
│   │   ├── transforms.py    # Color transforms
│   │   ├── bayer.py         # Bayer/RAW support
│   │   ├── multi_layer.py   # Multi-layer images
│   │   └── metadata.py      # Metadata utilities
│   ├── streaming/       # Progressive/streaming support
│   └── utils/           # Helper functions
├── cross_codec/         # SDK wrapper and validation tools
├── tests/               # Test suite (385+ tests)
├── examples/            # Example scripts
├── notes/               # Documentation
└── gfwx-sdk/            # Reference C++ SDK

Documentation

Running Tests

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=pygfwx

# Run specific test file
uv run pytest tests/test_bitstream.py

# Run SDK comparison tests (requires built SDK)
uv run pytest -m sdk

Development

Code Quality

# Check linting
uv run ruff check src tests

# Format code
uv run ruff format src tests

Building the SDK

The reference SDK is needed for validation tests:

cd gfwx-sdk/build
cmake ..
cmake --build . --config Release

Quality Levels

Quality Description Use Case
1-64 High compression Thumbnails, previews
64-256 Medium-high Web images
256-512 Medium General photography
512-1000 Low compression High quality
1024 Lossless Archival, masters

How It Works

GFWX uses a wavelet-based compression pipeline:

  1. Color Transform - Convert RGB to decorrelated color space (UYV)
  2. Wavelet Transform - Apply lifting-based wavelet decomposition
  3. Quantization - Scale coefficients based on quality setting
  4. Entropy Coding - Golomb-Rice coding with context modeling
┌─────────┐    ┌─────────┐    ┌────────┐    ┌─────────┐
│  Image  │ -> │  Color  │ -> │Wavelet │ -> │Quantize │ -> ┌──────────┐
│  RGB    │    │Transform│    │ Lift   │    │ Scale   │    │ Entropy  │
└─────────┘    └─────────┘    └────────┘    └─────────┘    │  Code    │
                                                           └────┬─────┘
                                                                │
                                                                v
                                                         ┌──────────┐
                                                         │Compressed│
                                                         │  Data    │
                                                         └──────────┘

References

License

BSD 3-Clause License (same as GFWX)

Acknowledgments

  • Graham Fyffe for creating GFWX
  • USC Institute for Creative Technologies
  • GFWX is "thoroughly tested using several pictures of cats" 🐱

This is an educational project focused on understanding wavelet-based image compression.

Metadata

Release files for pygfwx 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygfwx 0.1.2
File Size Uploaded
pygfwx-0.1.2.tar.gz 5.9 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygfwx 0.1.2
File Interpreter ABI Platform
pygfwx-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.0 MB

Release files / pygfwx-0.1.2.tar.gz

Download URL pygfwx-0.1.2.tar.gz
Size 5.9 MB
Tags Source
SHA-256 checksum
How to use checksums
0b4ecbad9ffe20371ede40cc6532015029903a62568e1a4b90ea85922714eb97
BLAKE2b-256 checksum
How to use checksums
5bfb09a1a06d1674b1c555b16432148b22a259bc4020eda394416718305e681f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 31, 2026.

Transparency log

Release files / pygfwx-0.1.2-py3-none-any.whl

Download URL pygfwx-0.1.2-py3-none-any.whl
Size 76.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
275f61054b3c1ba3567dfbac0d2fb27137abef0d38f9f1b16ecddffd4bf35112
BLAKE2b-256 checksum
How to use checksums
d3935d6fd8407cd5b0a79e78dce675316f6be18c42be93cb1df74b44404a13f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 31, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page