Flask Image Compressor
A Flask extension that provides automatic image compression and optimization for your static images.
Features
- Automatic image compression for static images
- Support for multiple image formats (JPEG, PNG, WebP)
- Global compression quality settings
- Per-image compression quality override
- Image resizing with aspect ratio preservation
- WebP support with JPEG/PNG fallback
- Progressive JPEG loading
- PNG optimization with transparency support
- Responsive images with srcset
- Lazy loading support
- Caching of compressed images
- Easy integration with Flask applications
Installation
pip install flask-image-compressor
Quick Start
Basic Usage
from flask import Flask
from flask_image_compressor import ImageCompressor
# Create the compressor instance
compressor = ImageCompressor()
def create_app():
app = Flask(__name__)
# Configure the app
app.config.update({
'IMAGE_COMPRESSION_QUALITY': 75, # Default compression quality
'IMAGE_MAX_WIDTH': 1920, # Default max width
'IMAGE_MAX_HEIGHT': 1080, # Default max height
'IMAGE_WEBP_ENABLED': True, # Enable WebP support
'IMAGE_PROGRESSIVE': True, # Enable progressive JPEGs
'IMAGE_PNG_OPTIMIZE': True, # Enable PNG optimization
'IMAGE_PNG_COMPRESSION_LEVEL': 6 # PNG compression level (0-9)
})
# Initialize the compressor with the app
compressor.init_app(app)
return app
app = create_app()
Using in Templates
<!-- Basic compression with WebP support -->
{{ compressed_image('images/photo.jpg') | safe }}
<!-- Compressed with specific size -->
{{ compressed_image('images/photo.jpg', max_width=800, max_height=600) | safe }}
<!-- Responsive images with multiple sizes -->
{{ responsive_image('images/photo.jpg') | safe }}
<!-- WebP with specific quality -->
{{ webp_image('images/photo.jpg', quality=80) | safe }}
<!-- Disable lazy loading -->
{{ compressed_image('images/photo.jpg', lazy=False) | safe }}
Configuration Options
| Option | Default | Description |
|---|---|---|
IMAGE_COMPRESSION_QUALITY |
85 | Default compression quality (0-100) |
IMAGE_MAX_WIDTH |
1920 | Maximum width for resized images |
IMAGE_MAX_HEIGHT |
1080 | Maximum height for resized images |
IMAGE_WEBP_ENABLED |
True | Enable WebP format support |
IMAGE_PROGRESSIVE |
True | Enable progressive JPEG loading |
IMAGE_PNG_OPTIMIZE |
True | Enable PNG optimization |
IMAGE_PNG_COMPRESSION_LEVEL |
6 | PNG compression level (0-9) |
Advanced Usage
Responsive Images
Generate multiple sizes for different viewports:
{{ responsive_image('images/photo.jpg',
sizes=[(400, 90), (800, 85), (1200, 80), (1600, 75)]) | safe }}
Custom Compression
# In your route
@app.route('/image')
def get_image():
return render_template('image.html',
image=compressor.compressed_image('images/photo.jpg',
quality=60,
max_width=800,
lazy=False
)
)
How it Works
-
When an image is requested:
- Checks if a compressed version exists in cache
- If not, creates a new compressed version
- Applies format-specific optimizations
- Generates WebP version if enabled
- Caches the results for future use
-
Format-specific optimizations:
- JPEG: Quality compression, progressive loading
- PNG: Compression level, transparency preservation
- WebP: Generated for JPEGs with fallback
Development
Building the Package
# Install build tools
pip install twine wheel
# Build the package
./build_pypi.sh
# Upload to PyPI
twine upload dist/*
License
MIT License
Metadata
Release files for flask-image-compressor 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_image_compressor-0.1.0.tar.gz | 5.3 kB | Without provenance | Details for flask_image_compressor-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_image_compressor-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for flask_image_compressor-0.1.0-py3-none-any.whl |
Total release size: 10.9 kB
Release files / flask_image_compressor-0.1.0.tar.gz
| Download URL | flask_image_compressor-0.1.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
519de7950f4f5589e693fd730c9390614ba927cc0ea43b16bd3d9f7b63991a5e
|
|
BLAKE2b-256 checksum How to use checksums |
0b44be6ef52c40ffb8c4a211909e33cce851400f636431597ee79596ee7ec2c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / flask_image_compressor-0.1.0-py3-none-any.whl
| Download URL | flask_image_compressor-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f244a008daa8b48a54bd708bc3d9991547aebe3e12ffc3322c84524fb2861d0c
|
|
BLAKE2b-256 checksum How to use checksums |
b9aeecc72152cb592437d5f151ed7843b3fe4f2215814a62811597fe0ec2bf20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for flask-image-compressor 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.