DODI: Django On-Demand Images
Creates modified images when they are requested via http, not at page request / template rendering time. Works with both static files and media files.
Installation:
pip install dodi
urls.py:
urlpatterns = [
...,
path('dodi/', include('dodi.urls')),
...,
]
Usage
Just request images via urls with the format /dodi/TRANSFORM?SOURCE.
SOURCE is the site-relative url of a self-hosted static file or media file.
TRANSFORM is a semicolon-separated list of options (key=value pairs) and flags. Available transforms/flags are described below.
IE: /dodi/width=400;height=300;match_orientation?/static/my_app/logo.png
While you could construct these urls manually, we provide a few utility functions that you should use instead: dodi.url(), dodi.static_url(), and dodi.url_base(). See src/dodi/_utils.py for details.
In Django Templates
We haven't added template tags yet, because we use html_generators rather than django templates. Might add later.
Explicit Cropping
You can specify any of crop_left, crop_right, crop_top, crop_bottom. Pass either:
- an integer, representing the number of pixels to crop from that edge
- a decimal between 0 and 1, representing the fraction of the width/height to crop from that edge
crop_left=50;crop_top=100: crop 50px from the left edge, and 100px from the top.
This happens before any resizing.
Resizing
To resize, you must specify width and/or height (as integers). There are 3 "modes" of resizing
mode=scale (default)
width=300;height=200 / width=300;height=200;mode=scale
Scale the image to fit within 300x200 pixels. If the aspect ratio doesn't match, one dimension will be smaller than.
mode=crop
width=300;height=200;mode=crop
Center-crop the image to a 3x2 aspect ratio, then scale to size.
mode=stretch
width=300;height=200;mode=stretch
Stretch/distort the image so that it's 300x200, without cropping.
Resize Flags
There are some flags that affect resizing. They affect all modes the same way.
scale_up
width=1600;height=900;scale_up
By default, we'll never increase either of an image's dimensions. If this flag is set, we will.
match_orientation
width=300;height=200;mode=crop;match_orientation
If the image is landscape (width>height), then return a 300x200 image. If it's portrait, return a 200x300 image. Useful when you have a collection of landscape/portrait images, and you want them all to have the same area.
Other Manipulations
overlay
Add a semi-transparent colored overlay over top of the image.
Takes a "color,alpha" pair, where 0 <= alpha <= 1, and color is any CSS color that can be understood by PIL.
Ie. overlay=green,0.5 or overlay=#000,0.8
color / contrast / brightness
All take values from 0 to 1. All default to 1.
color=0: black and white, color=1: original image
contrast=0: solid gray, contrast=1: original image
brightness=0: solid black, brightness=1: original image
rotate
Allowed values are 0, 90, 180, 270.
rotate=90: rotate 90 degrees counter-clockwise
Caching / Immutable Sources
We don't cache images at all. Instead, we send far-future expiry headers. We send an ETag header and handle conditional requests properly.
Web browsers will cache our images. You can configure your web server to cache them, too, so we never have to generate the same image twice.
This all relies on your source urls being "immutable". That is, we expect the image at a given static or media url to never change. This will work automatically for static files if you use ManifestStaticFilesStorage (django's default when DEBUG=False). For media files, it means you should be careful to set the upload_to option such that the upload path/name is guaranteed to change every time the field is changed (a new image is uploaded).
Multiple Domains / DODI_BASE_URL
If you're running your site under multiple domains (ie. en.foo.com and fr.foo.com), they are likely serving the same static/media files. If you have your web server set up to cache responses, it likely uses the absolute url as the cache key.
Even though en.foo.com/dodi/TRANSFORM/SOURCE and fr.foo.com/dodi/TRANSFORM/SOURCE refer to the same image, they will be generated and cached separately.
In this case, (in your settings.py) you should set DODI_BASE_URL to an absolute url (ie. https://en.foo.com). With this setup, all of our url-generating utilities will generate absolute urls, so that images are always requested from the same domain.
Authorization
TODO - document how to use dodi.validator if user wants to add stricter validation/authorization.
Tests
We have two types of tests setup.
python -m tests.test_basic runs through an automated set of tests. tox will run these in all of our test environments.
We also have visual tests, requiring you to verify correctness. python manage.py runserver 8000, then go open localhost:8000 in your web browser, and verify that the transformed images are as expected.
TODO
Support for "focal point" (focal_point_left and focal_point_bottom) -> affects how mode=crop behaves.
SVG to PNG support
validator documentation and tests
Metadata
Release files for dodi 2.5.1
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 | |
|---|---|---|---|---|
| dodi-2.5.1.tar.gz | 32.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dodi-2.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 76.5 kB
Release files / dodi-2.5.1.tar.gz
| Download URL | dodi-2.5.1.tar.gz |
|---|---|
| Size | 32.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15a001eeb05268ef7998dc2d9dfe4746ea0079673593ded1f6f581b753b347f8
|
|
BLAKE2b-256 checksum How to use checksums |
9163bce403d76888ca61e627c2a6b722a1c13acbdffa54a10fc45697bb5f8a1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.10
|
Release files / dodi-2.5.1-py3-none-any.whl
| Download URL | dodi-2.5.1-py3-none-any.whl |
|---|---|
| Size | 44.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
334e1f52e35650e42e599183ad940795a5ed7392e8cb14c0944cb2e79f7fcc68
|
|
BLAKE2b-256 checksum How to use checksums |
4936405311b6eb0221fd477ad0f23e545b64ecd22fce08611ec2801056e7464f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.10
|
Security for dodi 2.5.1
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.