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.2.2.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.2.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: splathash_py-1.2.2.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.2.2.tar.gz
Algorithm Hash digest
SHA256 b7f7235247562388cc69aae2c0ad5c25976a87e1bba0887a4cf98de8bc4c7eac
MD5 0f5efe698b46fee684c3549c60c38cb4
BLAKE2b-256 b1537f5bce758145a98c3dad553b386be86c161df6f2dfc310780b7bd35fa73b

See more details on using hashes here.

Provenance

The following attestation bundles were made for splathash_py-1.2.2.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.2.2-py3-none-any.whl.

File metadata

  • Download URL: splathash_py-1.2.2-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.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 456c84c29f5637ca48cfd1f249d6cfb6c49bf89396fe96f136979e87adf3f1b8
MD5 8559884c5c86c6d310d431fe7f7f7a8c
BLAKE2b-256 9626bbed2407931e4c670aecb9080be180996eee9b707509a3bdefa5c4bb463f

See more details on using hashes here.

Provenance

The following attestation bundles were made for splathash_py-1.2.2-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