Generate blurred image placeholders for lazy loading
Project description
Blur Placeholder
A simple, fast, and lightweight Python library to generate base64-encoded blurred image placeholders for lazy loading. This helps improve user experience by showing a low-quality version of an image while the full-quality version is loading.
Key Features
- Multiple Image Sources: Generate placeholders from URLs, local file paths, or raw image bytes.
- Asynchronous Support: Built-in async support using
aiohttpfor non-blocking operations. - Caching: In-memory caching to avoid regenerating placeholders for the same image.
- Customizable: Control the blur intensity, quality, and image format.
- Lightweight: Minimal dependencies (
Pillowandrequests).
Installation
Install the basic version with synchronous support:
pip install blur-placeholder
For asynchronous support, install the async extra:
pip install blur-placeholder[async]
Usage
Synchronous
The synchronous generate_blur_placeholder function is ideal for simple scripts and applications where blocking operations are acceptable.
from blur_placeholder import generate_blur_placeholder
# From a URL
data_url = generate_blur_placeholder("https://picsum.photos/seed/picsum/800/600")
print(data_url)
# From a local file
# with open("my_image.jpg", "wb") as f:
# f.write(requests.get("https://picsum.photos/seed/picsum/800/600").content)
# data_url = generate_blur_placeholder("my_image.jpg")
# print(data_url)
# From image bytes
# with open("my_image.jpg", "rb") as f:
# image_bytes = f.read()
# data_url = generate_blur_placeholder(image_bytes)
# print(data_url)
Asynchronous
The asynchronous generate_blur_placeholder_async function is perfect for high-performance applications using asyncio.
import asyncio
from blur_placeholder import generate_blur_placeholder_async
async def main():
# From a URL
data_url = await generate_blur_placeholder_async("https://picsum.photos/seed/picsum/800/600")
print(data_url)
if __name__ == "__main__":
asyncio.run(main())
Advanced Usage
You can customize the placeholder generation with several optional parameters:
data_url = generate_blur_placeholder(
"https://picsum.photos/seed/picsum/800/600",
blur_width=25, # Width of the blurred image (default: 20)
blur_radius=5, # Blur radius (default: 2)
quality=40, # JPEG quality (default: 30)
image_format="WEBP" # Output format (default: "JPEG")
)
API Reference
generate_blur_placeholder(image_source, **kwargs)
image_source(str | bytes): The source of the image. Can be a URL, a local file path, or image data in bytes.blur_width(int, optional): The width to resize the image to before blurring. Defaults to20.blur_radius(int, optional): The radius for the Gaussian blur. Defaults to2.quality(int, optional): The quality of the output JPEG image (1-100). Defaults to30.cache(BaseCache, optional): A custom cache object. Defaults to a built-in in-memory cache.timeout(int, optional): The cache timeout in seconds. Defaults to86400(24 hours).image_format(str, optional): The output image format (e.g., "JPEG", "WEBP", "PNG"). Defaults to"JPEG".
generate_blur_placeholder_async(image_source, **kwargs)
This function has the same parameters as the synchronous version but is an async function.
Caching
By default, blur-placeholder caches results in memory to speed up repeated requests for the same image. You can provide your own cache implementation by creating a class that inherits from BaseCache (for sync) or BaseAsyncCache (for async) and passing an instance to the cache parameter.
Contributing
Contributions are welcome! To get started:
- Fork the repository.
- Clone your fork:
git clone https://github.com/your-username/blur-placeholder.git - Install the development dependencies:
pip install -e ".[dev]" - Run the tests:
pytest
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blur_placeholder-0.1.2.tar.gz.
File metadata
- Download URL: blur_placeholder-0.1.2.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f54b05d705611746bb08b745bb9f0ea7d5c21a2174adb54f957983ad4f0d7fc
|
|
| MD5 |
993c8aeb53c5bd6a208ffba5e13d319f
|
|
| BLAKE2b-256 |
c762b7a4834e05426df08992f41daa4806f678a9a9f18dcee3f58fe7190fbec8
|
File details
Details for the file blur_placeholder-0.1.2-py3-none-any.whl.
File metadata
- Download URL: blur_placeholder-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd65de2468dd5d04d3ff1a82867b1cd3d50a83b12b1d8ff391cd7bea4554b1a0
|
|
| MD5 |
9b900c3faf416bd74a2f7c092772d816
|
|
| BLAKE2b-256 |
c104d45f4706e9f61005d35f33f6eb5bcb8304fd18de6be3ea1a00e036970bae
|