Skip to main content

A Python implementation of the Thumbhash image placeholder generation algorithm.

Project description


ThumbHash for Python

Test Coverage Package version Supported Python versions MIT License

Introduction

The thumbhash library implements the Thumbhash image placeholder generation algorithm invented by Evan Wallace in Python.

A full explanation and interactive example of the algorithm can be found at https://github.com/evanw/thumbhash

Installation

You need Python 3.7+.

$ pip install thumbhash-python

Usage

Create thumbhash from image file:

from thumbhash import image_to_thumbhash

with open('image.jpg', 'rb') as image_file:
    hash = image_to_thumbhash(image_file)

You can also pass file name as parameter to the function:

from thumbhash import image_to_thumbhash

hash = image_to_thumbhash('image.jpg')

These functions use the Pillow library to read the image.

If you want to directly convert a rgba array to a thumbhash, you can use the low-level function:

from thumbhash.encode import rgba_to_thumbhash

rgba_to_thumbhash(w: int, h: int, rgba: Sequence[int]) -> bytes

To decode a thumbhash into an image:

from thumbhash import thumbhash_to_image

image = thumbhash_to_image("[THUMBHASH]", base_size=128)

image.show()

image.save('path/to/file.png')

Alternatively you can use the following function to deal directly with the pixels array (without relying on Pillow):

from thumbhash.decode import thumbhash_to_rgba

def thumbhash_to_rgba(
    hash: bytes, base_size: int = 32, saturation_boost: float = 1.25
) -> Tuple[int, int, List[int]]

CLI

You can also use the CLI mode to encode or decode directly via your shell.

Usage:

$ thumbhash [OPTIONS] COMMAND [ARGS]...

Options:

  • --install-completion: Install completion for the current shell.
  • --show-completion: Show completion for the current shell, to copy it or customize the installation.
  • --help: Show this message and exit.

Commands:

  • decode: Save thumbnail image from thumbhash
  • encode: Get thumbhash from image

thumbhash decode

Save thumbnail image from thumbhash

Usage:

$ thumbhash decode [OPTIONS] IMAGE_PATH HASH

Arguments:

  • IMAGE_PATH: The path where the image created from the hash will be saved [required]
  • HASH: The base64-encoded thumbhash [required]

Options:

  • -s, --size INTEGER RANGE: The base size of the output image [default: 32; x>=1]
  • --saturation FLOAT: The saturation boost factor to use [default: 1.25]
  • --help: Show this message and exit.

thumbhash encode

Get thumbhash from image

Usage:

$ thumbhash encode [OPTIONS] IMAGE_PATH

Arguments:

  • IMAGE_PATH: The path of the image to convert [required]

Options:

  • --help: Show this message and exit.

Contributing

See Contributing documentation

License

thumbhash-python is distributed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thumbhash_python-1.0.1.tar.gz (54.4 kB view details)

Uploaded Source

Built Distribution

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

thumbhash_python-1.0.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file thumbhash_python-1.0.1.tar.gz.

File metadata

  • Download URL: thumbhash_python-1.0.1.tar.gz
  • Upload date:
  • Size: 54.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for thumbhash_python-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9212871c5ddf10487148c767ebd8785c8010f79e378709805bc8729d4293e03d
MD5 68fca0b1d23d4b5a4bf5dff3d4909290
BLAKE2b-256 6f29bcb4a01ce7faab0888b00f2eb73e1a6a18b241549ebda61e8e5b31424a37

See more details on using hashes here.

File details

Details for the file thumbhash_python-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for thumbhash_python-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b1bc4ac08ff4354dba70a0caf85a9b3f3522181fbd3e05eb4f03aa786a36578b
MD5 09c90ab7e3e776f9afe08d217058d832
BLAKE2b-256 3d0eb77ae42436765e3e6323a02df99d6999b82edea7123530448979bbe97045

See more details on using hashes here.

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