Skip to main content

Numba aware BlurHash encoder and decoder implementation for Python

Project description

Blurhash numba logo

blurhash-numba : The fastest Python 3 BlurHash implementation powered by numba

Build Status

What is BlurHash?

BlurHash is a compact representation of a placeholder for an image.

BlurHash encoder consumes an image, and provides a short string (only 20-30 characters!) that represents the placeholder for the image. You perform this on the backend of your service, and store the string along with the image. When you send data to any client, you send both the URL to the image, and the BlurHash string. Your client then takes the string, and decodes it into an image that it shows while the real image is loading over the network. The string is short enough that it comfortably fits into whatever data format you use. For instance, it can easily be added as a field in a JSON object.

In summary:

   

Read more about the algorithm here.

Installation

You can install blurhash-numba using pip3

$ pip3 install blurhash-numba

You can also optionally install Pillow (PIL) along with blurhash-numba in case it is not already installed

$ pip3 install blurhash-numba[pillow]

Usage

Encoding

As blurhash_numba.encode accepts the image in the form of a numpy array. You can convert an image file using the Pillow python library.

from blurhash_numba import encode
from PIL import Image
import numpy as np

image = Image.open("256.jpg")

image

image_array = np.array(image.convert("RGB"), dtype=np.float)
blurhash_code = encode(image_array, x_components = 4, y_components = 3)

blurhash_code
'LtL#LZR*x]jG.TRkoeayIUofM{R*'

y_components and x_components parameters adjust the amount of vertical and horizontal AC components in hashed image. Both parameters must be >= 1 and <= 9.

Decoding

from blurhash_numba import decode
from PIL import Image
import numpy as np

blur_img = Image.fromarray(np.array(decode(blur_hash, 256, 256)).astype('uint8'))

blur_img

Tests

Run test suite with pytest in virtual environment

$ pytest

FAQs

Why should I use blurhash-numba?

This is the fastest implementation of the BlurHash algorithm (both encoding & decoding) in Python currently as it uses numba to directly convert the Python+NumPy code into fast machine code. It is 30-70x faster than halcy/blurhash-python and 2-4x faster than woltapp/blurhash.

How do I pick the number of X and Y components?

It depends a bit on taste. The more components you pick, the more information is retained in the placeholder, but the longer the BlurHash string will be. Also, it doesn't always look good with too many components. We usually go with 4 by 3, which seems to strike a nice balance.

However, you should adjust the number of components depending on the aspect ratio of your images. For instance, very wide images should have more X components and fewer Y components.

What is the punch parameter in this implementations?

It is a parameter that adjusts the contrast on the decoded image. 1 means normal, smaller values will make the effect more subtle, and larger values will make it stronger. This is basically a design parameter, which lets you adjust the look.

Technically, what it does is scale the AC components up or down.

Credits

This project is based on the pure python BlurHash implementation by halcy/blurhash-python.
Also credit goes to the original implementation by woltapp/blurhash.

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

blurhash-numba-0.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

blurhash_numba-0.0.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file blurhash-numba-0.0.1.tar.gz.

File metadata

  • Download URL: blurhash-numba-0.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for blurhash-numba-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d5a1edbc3ec182f229992b9dee65f243ed732173548992262cdce81456c2be14
MD5 7acb0b161dd5f6780692e56d0086d555
BLAKE2b-256 fac8b4ed70da73de95aaf81ae577a0c33a0c44dd1974eb91b361d12bc300bdb9

See more details on using hashes here.

File details

Details for the file blurhash_numba-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: blurhash_numba-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for blurhash_numba-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd29f128b39b6061f5476d4122e0ac57d620ac24787563daa8fdda476afe2343
MD5 033c3a69f9f87cce7e3ebd8a925d125e
BLAKE2b-256 411e90c0fe560648c4cc8ec6b6ffe79fd0a72493bd4e487b0c71f1fe26dc9553

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page