Skip to main content

SplatHash: compress any image to 16 bytes and reconstruct it

Project description

SplatHash — Python

Python implementation of SplatHash: compress any image to 16 bytes and reconstruct it.

Requirements

  • Python 3.8+
  • Pillow (for loading image files)

Installation

pip install splathash-py

For local development from the repo:

pip install -e src/py

Usage

from splathash import encode, encode_raw, decode

# Encode from a file path
with open("photo.jpg", "rb") as f:
    hash_bytes = encode(f)          # bytes, always 16 bytes

# Or from a file path string directly
hash_bytes = encode("photo.jpg")

# Or from a PIL Image
from PIL import Image
img = Image.open("photo.jpg")
hash_bytes = encode(img)

# Encode from raw RGBA bytes
hash_bytes = encode_raw(rgba_bytes, width, height)

# Decode to 32×32 RGBA
pixels = decode(hash_bytes)         # bytes, 32 * 32 * 4 = 4096 bytes

See example.py for a full working example that encodes a directory of images.

API

encode(source) -> bytes

Encodes an image to a 16-byte SplatHash.

source can be:

  • A file path (str or Path)
  • A file-like object (binary mode)
  • A PIL.Image.Image

Returns bytes of exactly 16 bytes.

encode_raw(rgba: bytes, width: int, height: int) -> bytes

Encodes raw RGBA pixel data. rgba must be width * height * 4 bytes.

Returns bytes of exactly 16 bytes.

decode(hash_bytes: bytes) -> bytes

Decodes a 16-byte SplatHash to a 32×32 RGBA image.

Returns bytes of exactly 32 * 32 * 4 = 4096 bytes (raw RGBA, row-major).

Raises ValueError if hash_bytes is not exactly 16 bytes.

Converting the decoded bytes to an image

from PIL import Image
from splathash import encode, decode

hash_bytes = encode("photo.jpg")
pixels = decode(hash_bytes)

img = Image.frombytes("RGBA", (32, 32), pixels)
img.save("preview.png")

Testing

pip install pytest Pillow
pytest test_splathash.py -v

Or via mise from the repo root:

mise run test:py

How It Works

See ALGORITHM.md for the full technical specification.

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

splathash_py-1.3.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

splathash_py-1.3.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file splathash_py-1.3.0.tar.gz.

File metadata

  • Download URL: splathash_py-1.3.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for splathash_py-1.3.0.tar.gz
Algorithm Hash digest
SHA256 eec5a695cd8ad6525fb091bc365e45198d04307ac65632aec9254af2448e30e7
MD5 d46e369e66deb202ce0fb408862aa5f6
BLAKE2b-256 2b9dec7c71eb141c13639c1478001fefffc8e842d94e593543daf88a9494e4ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for splathash_py-1.3.0.tar.gz:

Publisher: release.yml on junevm/splathash

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

File details

Details for the file splathash_py-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: splathash_py-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for splathash_py-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c5d31a9744b4756c60d0177051485996c4e625edd6338d5705d69444538b851
MD5 c2c6f5011c0433faa9e11c69bfbf50b8
BLAKE2b-256 68ba4e69a326c0daf38940451afc4b7c6f750f49936c8477d3189253faea0e7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for splathash_py-1.3.0-py3-none-any.whl:

Publisher: release.yml on junevm/splathash

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