Metadata-Version: 2.4
Name: blurhash-rs
Version: 3.0.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Dist: pillow>=9.0.0
License-File: LICENSE-THIRD-PARTY.md
License-File: LICENSE.md
Summary: BlurHash encoder and decoder for Python
Keywords: blurhash,decoder,encoder,image,loading,placeholder,preview,progressive
Author-email: Kamil Monicz <kamil@monicz.dev>
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Issues, https://github.com/Zaczero/pkgs/issues
Project-URL: Repository, https://github.com/Zaczero/pkgs/tree/main/blurhash-rs

# blurhash-rs

Encode images as compact [BlurHash](https://blurha.sh) placeholders and decode
them into [Pillow](https://python-pillow.org/) previews or raw RGB bytes.
Written in Rust, for Python 3.11+.

## Installation

```sh
pip install blurhash-rs
```

## Encode and decode an image

```py
from PIL import Image
from blurhash_rs import decode, encode

image = Image.new("RGB", (24, 16), (40, 100, 180))
blurhash = encode(image)
preview = decode(blurhash, 32, 24)

print(blurhash)
```

`preview` is a 32 × 24 RGB image. For raw 8-bit RGB buffers without importing
Pillow, use `encode_rgb` and `decode_rgb`.

## Encode an image file

```py
from PIL import Image
from blurhash_rs import encode

# Use your image's path.
with Image.open("photo.jpg") as opened:
    image = opened.copy()

image.thumbnail((100, 100))
blurhash = encode(image, x_components=4, y_components=3)
```

You can also pass a filename directly to `encode`. Images you supply remain
open and unchanged. Alpha is discarded, so composite transparent images onto
your chosen background first.

## Benchmarks

**About 125–225× faster encoding and 13–21× faster decoding** than
[`blurhash-python`](https://github.com/woltapp/blurhash-python) for 32–128 pixel
square RGB [Pillow](https://python-pillow.org/) images using 4 by 3 components.

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="benchmarks/encode-dark.svg">
  <img src="https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/encode.svg" alt="Median time to encode 32, 64, and 128 pixel square RGB images with 4 by 3 components.">
</picture>

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="benchmarks/encode-workloads-dark.svg">
  <img src="https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/encode-workloads.svg" alt="Median time to encode landscape and portrait images, 1 by 1 and 9 by 9 component cases, and RGBA or grayscale input.">
</picture>

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="benchmarks/decode-dark.svg">
  <img src="https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/decode.svg" alt="Median time to decode 32, 64, and 128 pixel square RGB images with 4 by 3 components.">
</picture>

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="benchmarks/decode-workloads-dark.svg">
  <img src="https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/decode-workloads.svg" alt="Median time to decode landscape and portrait output sizes and 1 by 1 or 9 by 9 component cases.">
</picture>

| Workload, median time per image | blurhash-rs 2.0.1 | blurhash 1.1.5 | blurhash-python 1.2.2 |
| --- | ---: | ---: | ---: |
| Encode 32 × 32 square RGB, 4 × 3 | 4.47 µs | 5.29 ms | 559 µs |
| Encode 64 × 64 square RGB, 4 × 3 | 9.59 µs | 21.7 ms | 2.16 ms |
| Encode 128 × 128 square RGB, 4 × 3 | 39 µs | 81.3 ms | 7.95 ms |
| Decode 32 × 32 square RGB, 4 × 3 | 13.3 µs | 4.84 ms | 172 µs |
| Decode 64 × 64 square RGB, 4 × 3 | 34.1 µs | 26.4 ms | 652 µs |
| Decode 128 × 128 square RGB, 4 × 3 | 120 µs | 81.7 ms | 2.51 ms |

All three encoders produced identical hashes; decoded RGB channels differed by
at most one 8-bit level. Measurements used CPython 3.15.1, Pillow 13.0.0, NumPy 2.5.4,
and a shared Ryzen 9 5950X workstation running Linux 7.3.5. Table values are
medians of twelve worker-process means; chart whiskers show 95% bootstrap intervals.

[Full workload medians](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/results.md) ·
Raw [PyPerf](https://pyperf.readthedocs.io/) results: [blurhash-rs](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/blurhash-rs.json),
[blurhash](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/blurhash.json), and
[blurhash-python](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmarks/blurhash-python.json) ·
[Benchmark source](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/benchmark.py)

## API reference

### `encode` and `encode_rgb`

Pass a Pillow `Image` or a source accepted by `Image.open` to `encode`, or raw
RGB bytes with positive dimensions to `encode_rgb`. The `x_components` and
`y_components` counts, each from 1 through 9 (default `4` and `3`), control the
hash's detail.

### `decode` and `decode_rgb`

Pass a hash and positive output dimensions to get an RGB Pillow `Image`
(`decode`) or packed RGB bytes (`decode_rgb`). Use finite, nonnegative `punch`
values (default `1.0`) to control contrast: zero shows the constant DC color,
values between zero and one reduce contrast, and values above one increase it.

### `BlurhashDecodeError`

This exception reports an invalid hash, image dimension, or contrast scale
from `decode`. Its `blurhash` attribute contains the hash passed to the
decoder.

[Full reference](https://raw.githubusercontent.com/Zaczero/pkgs/blurhash-rs/3.0.0/blurhash-rs/python/blurhash_rs/__init__.py) ·
[Issues and questions](https://github.com/Zaczero/pkgs/issues) ·
[Private security reports](https://github.com/Zaczero/pkgs/security/advisories/new) ·
[Premium support](https://monicz.dev/#get-in-touch)

