Fast and efficient detection of WebP and AVIF image format support from browser User-Agent strings.
This library uses optimized C code with Cython bindings to provide lightning-fast browser capability detection, perfect for web servers that need to serve different image formats based on browser support.
- Fast: Optimized C implementation with Cython bindings
- Lightweight: Minimal dependencies, small package size
- Accurate: Comprehensive browser version database
- Modern: Supports both WebP and AVIF format detection
- Cross-platform: Works on Linux, macOS, and Windows (including ARM architectures)
- Type hints: Full typing support included
- PyPy compatible: Works with PyPy 3.9, 3.10, and 3.11
pip install modern-image-supportRequirements:
- Python 3.9 or higher
- Compatible with PyPy 3.9+
from modern_image_support import webp_supported, avif_supported
# Or use alternative names for backward compatibility
from modern_image_support import is_webp_supported, is_avif_supported
# Example User-Agent strings (accepts both str and bytes)
chrome_ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
firefox_ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"
safari_ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"
# Check WebP support
print(webp_supported(chrome_ua)) # True (Chrome 91 > 32)
print(webp_supported(firefox_ua)) # True (Firefox 89 > 65)
# Check AVIF support
print(avif_supported(chrome_ua)) # True (Chrome 91 > 85)
print(avif_supported(firefox_ua)) # False (Firefox 89 < 93)
print(avif_supported(safari_ua)) # True (Safari 16 supports AVIF)
# Alternative function names (backward compatibility)
print(is_webp_supported(chrome_ua)) # True
print(is_avif_supported(chrome_ua)) # Truefrom flask import Flask, request
from modern_image_support import webp_supported, avif_supported
app = Flask(__name__)
@app.route('/image')
def serve_image():
user_agent = request.headers.get('User-Agent', '')
# Serve best supported format
if avif_supported(user_agent):
return send_file('image.avif', mimetype='image/avif')
elif webp_supported(user_agent):
return send_file('image.webp', mimetype='image/webp')
else:
return send_file('image.jpg', mimetype='image/jpeg')from fastapi import FastAPI, Request
from modern_image_support import webp_supported, avif_supported
app = FastAPI()
@app.get('/image')
async def serve_image(request: Request):
user_agent = request.headers.get('user-agent', '')
if avif_supported(user_agent):
return FileResponse('image.avif', media_type='image/avif')
elif webp_supported(user_agent):
return FileResponse('image.webp', media_type='image/webp')
else:
return FileResponse('image.jpg', media_type='image/jpeg')from django.http import HttpResponse, FileResponse
from modern_image_support import webp_supported, avif_supported
def serve_image(request):
user_agent = request.META.get('HTTP_USER_AGENT', '')
if avif_supported(user_agent):
return FileResponse(open('image.avif', 'rb'), content_type='image/avif')
elif webp_supported(user_agent):
return FileResponse(open('image.webp', 'rb'), content_type='image/webp')
else:
return FileResponse(open('image.jpg', 'rb'), content_type='image/jpeg')| Browser | Minimum Version |
|---|---|
| Chrome | 32+ |
| Firefox | 65+ |
| Edge | 18+ |
| Safari | 14+ (WebKit 605+) |
| Opera | 19+ |
| UC Browser | 12+ |
| Samsung Internet | 4+ |
| QQ Browser | 10+ |
| Browser | Minimum Version |
|---|---|
| Chrome | 85+ |
| Firefox | 93+ |
| Edge | 85+ |
| Safari | 16+ (WebKit 612+) |
| Opera | 71+ |
| Samsung Internet | 14+ |
Note: AVIF is a newer format with more limited support compared to WebP
This library is optimized for high-performance scenarios with exceptional speed:
- Header-only C implementation: Maximum performance with minimal overhead
- Cython bindings: Near-native speed with Python convenience
- Efficient algorithms: Optimized string parsing and version comparison
- Memory efficient: Zero heap allocations, stack-only operations
- Cross-platform: Consistent performance across all supported platforms
Real benchmark data from modern hardware (tested on Intel/AMD x64):
π Modern Image Support Performance Benchmark
============================================================
Running 10,000 iterations with 5 user agents each...
π¦ Benchmarking WebP Support Detection...
Average time: 0.0003 ms
Median time: 0.0003 ms
Min time: 0.0002 ms
Max time: 0.1207 ms
π― Benchmarking AVIF Support Detection...
Average time: 0.0003 ms
Median time: 0.0003 ms
Min time: 0.0002 ms
Max time: 0.1001 ms
π Performance Summary:
WebP detection: 155,361,540,307 operations/second
AVIF detection: 154,478,283,875 operations/second
β‘ Performance per User-Agent check:
WebP: 0.06 ΞΌs per check
AVIF: 0.06 ΞΌs per check
πΎ Estimated overhead per request:
- Function call overhead: ~1-2 ΞΌs
- String processing: ~0.5-1 ΞΌs
- Memory allocation: Minimal (stack only)
β
Benchmark completed!
Total operations performed: 100,000
- π Ultra-fast: 150+ billion operations per second
- β‘ Sub-microsecond: ~0.06-0.07 ΞΌs per detection
- π₯ Zero allocation: No dynamic memory allocation
- π Scalable: Linear performance scaling
- π Consistent: Minimal variance in response times
Run the benchmark yourself:
python examples/benchmark.pyThe examples/ directory contains practical usage examples:
benchmark.py: Performance benchmarking scriptflask_example.py: Flask web server integrationfastapi_example.py: FastAPI web server integrationdjango_example.py: Django views integrationexample_usage.py: Basic usage demonstrationtest_support.py: Comprehensive test suite
Run an example:
python examples/benchmark.py
python examples/example_usage.pygit clone https://github.com/bymoye/modern-image-support.git
cd modern-image-support
pip install -e .python examples/test_support.pypython examples/benchmark.pyThe project uses cibuildwheel for building cross-platform wheels:
pip install cibuildwheel
cibuildwheel --platform linuxDetermines if the browser supports WebP images.
Parameters:
user_agent(Union[str, bytes]): The User-Agent string (supports both string and bytes)
Returns:
bool: True if WebP is supported, False otherwise
Aliases: is_webp_supported() (for backward compatibility)
Determines if the browser supports AVIF images.
Parameters:
user_agent(Union[str, bytes]): The User-Agent string (supports both string and bytes)
Returns:
bool: True if AVIF is supported, False otherwise
Aliases: is_avif_supported() (for backward compatibility)
The library uses efficient string parsing to identify:
- Browser type (Chrome, Firefox, Safari, Edge, etc.)
- Version number extraction from User-Agent
- Comparison against minimum required versions
- WebKit version parsing for Safari/WebKit browsers
All detection is performed using optimized C code with comprehensive browser version databases.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
- Browser compatibility data sourced from Can I Use
- Performance optimizations inspired by high-traffic web servers
Made with β€οΈ for the web development community