Skip to main content

A library to generate BlurHash and PNG data URLs for AVIF images

Project description

BlurHash-AVIF

A Python library extending the Python BlurHash implementation to generate BlurHash strings and PNG data URLs for AVIF images.

Disclaimer: This is an unofficial extension and has no affiliation with the original BlurHash developers. All credit for the BlurHash concept and implementation goes to its creators.

Table of Contents

  1. Installation
  2. Features
  3. Requirements
  4. Usage
  5. Troubleshooting
  6. Contributing
  7. Attribution
  8. License

Installation

Install the library using pip:

pip install blurhash-avif

Features

  • Generate BlurHash strings from AVIF images
  • Create base64-encoded PNG data URLs from AVIF images
  • Maintain image aspect ratio during processing
  • Support RGB mode images
  • Optimize image loading for improved website performance

Requirements

  • Python 3.x
  • Pillow (PIL) library with AVIF support
  • NumPy library
  • base64 library
  • blurhash library

Usage

The library provides three main functions:

  1. generate_blurhash_from_avif: Create a BlurHash string from an AVIF image.
  2. generate_png_data_url_from_avif: Generate a base64-encoded PNG data URL from an AVIF image.
  3. generate_blurhash_and_data_url_from_avif: Produce both a BlurHash string and a PNG data URL.

Example Usage

from blurhash_avif import generate_blurhash_from_avif, generate_png_data_url_from_avif, generate_blurhash_and_data_url_from_avif

# Path to your AVIF file
avif_path = "path/to/your/image.avif"

# Generate BlurHash string
blurhash = generate_blurhash_from_avif(avif_path)
if blurhash:
    print(f"BlurHash: {blurhash}")
else:
    print("Failed to generate BlurHash")

# Generate PNG data URL
data_url = generate_png_data_url_from_avif(avif_path)
if data_url:
    print(f"PNG Data URL: {data_url[:50]}...") # Print first 50 characters
else:
    print("Failed to generate PNG Data URL")

# Generate both BlurHash and PNG data URL
blurhash, data_url = generate_blurhash_and_data_url_from_avif(avif_path)
if blurhash and data_url:
    print(f"BlurHash: {blurhash}")
    print(f"PNG Data URL: {data_url[:50]}...") # Print first 50 characters
else:
    print("Failed to generate BlurHash and PNG Data URL")

Troubleshooting

If you encounter issues with Pillow's AVIF support, try:

pip uninstall pillow
pip install "pillow[avif]"

you man need to install the aviflib library.

To install the required aviflib library, follow these steps:

On macOS (using Homebrew):

brew install libavif

On Ubuntu/Debian:

sudo apt-get install libavif-dev

On windows

pip install aom

or

On windows (vcpkg)

vcpkg install libavif

Contributing

We welcome contributions! To contribute:

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a pull request

Attribution

This package extends the Python BlurHash library. BlurHash was originally created by Dag Ågren for Wolt. The BlurHash algorithm and official implementations are available at the BlurHash GitHub repository.

License

This project is licensed under the Apache License, Version 2.0 with additional commercial terms. See the LICENSE file for full details.


Keywords: BlurHash, AVIF, image processing, Python, base64, PNG, data URL, image optimization, web performance, placeholder images

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_avif-0.4.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

blurhash_avif-0.4.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file blurhash_avif-0.4.0.tar.gz.

File metadata

  • Download URL: blurhash_avif-0.4.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for blurhash_avif-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7b64cdbab3045f4609554100981f90d5841ff13ffb67e9566aa5373d6b7268a7
MD5 e98e0f00094ca4cce4f3c92e3d646b5d
BLAKE2b-256 6f6607cf2297e2905e0c10db99250c7f69d5262228571d435db733295d492a87

See more details on using hashes here.

File details

Details for the file blurhash_avif-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for blurhash_avif-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 016eed10b3169dc6d651fc0e0e94327b51992d4b52bd8db5d15ff78951ee1317
MD5 4f6aaba9653055c8729e0caad167a6f8
BLAKE2b-256 3824d043cc4868a43c14c26c159ee3ed4fbbe5416b5ce23c10dd9dffe2e6516f

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