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

Uploaded Python 3

File details

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

File metadata

  • Download URL: splathash_py-1.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 23b82061987c5776147ff3c1bea14f7005c64769389bb2541b448feb6a97f089
MD5 963e37cc412c1825a5342380e09825c1
BLAKE2b-256 33962e22100cf6eb1e4c422d795cf15bddee13e7decfc2967067847c3122272a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: splathash_py-1.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c69a3f361adeeed9fb9cbb08ce70969222ba2a544238f9b57cc6bdbcc7e1801
MD5 35714f83b0a2f55b3b8366bdaf653e1a
BLAKE2b-256 ee126fe2655c7fb4763e7e9bb87becce9ef2008069e263dc27e89b2806172d00

See more details on using hashes here.

Provenance

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