Skip to main content

👁 Retina

Retina takes an image and a thumbnail alias and generates a dict with resized images. So if your easy_thumbnails alias looks something like this 'portrait': {'size': (150, 200)} you'll get the resized version of the original image plus a retina version of it.

image = File(user.profile_image).srcset('portrait')

# image holds now the following dict:
{
  'urls': {
    'default': [
      'path/to/images/profile_image__150x200.jpg',
      'path/to/images/profile_image__300x400.jpg',
    ]
  },
  'alt': 'John Doe',
}

Installation

First, install the latest release:

$ pip install retina

Register the adapters you want to use somewhere at boot:

from filer.models import Image as FilerImage
from filer.models import File as FilerFile

from retina.adapters.filer import FilerImageAdapter, FilerFileAdapter
from retina import manager

manager.update_adapters({
  FilerImage: FilerImageAdapter,
  FilerFile: FilerFileAdapter,
})

Or if you're using django with django-filer (which is the use-case Retina was developed for) this is even easier:

from retina import manager

manager.load_default_adapters()

Usage

Initiate the retina.File class with a django-filer File or Image model and call the srcset method with a easy_thumbnails alias as parameter on it. This will always return a dict with a urls and alt key. Where urls is again a dict of different sizes (default being the default if nothing else specified) and alt is just a string containing the alt text for the image. Retina tries to get the alt text by accessing any of the following properties on the filer.Image model: default_alt_text, name, original_filename

Basic

image = File(user.profile_image).srcset('portrait')

{
  'urls': {
    'default': [
      'path/to/images/profile_image__150x200.jpg',
      'path/to/images/profile_image__300x400.jpg',
    ]
  },
  'alt': 'John Doe',
}

Density

You can chain an additional density method to get a @3, @4, e.t.c version of the source image.

image = File(user.profile_image).density(3).srcset('portrait')

{
  'urls': {
    'default': [
      'path/to/images/profile_image__150x200.jpg',
      'path/to/images/profile_image__300x400.jpg',
      'path/to/images/profile_image__450x600.jpg',
    ]
  },
  'alt': 'John Doe',
}

Sizes

If you're website needs different image dimensions, say on mobile than on desktop, you can do that as well:

THUMBNAIL_ALIASES = {
  '': {
    'portrait_sm': {'size': (100, 100)},
    'portrait_xl': {'size': (150, 200)},
    }
},

image = File(user.profile_image).srcset('portrait', ['sm', 'xl'])

{
  'urls': {
    'sm': [
      'path/to/images/profile_image__100x100.jpg',
      'path/to/images/profile_image__200x200.jpg',
    ],
    'xl': [
      'path/to/images/profile_image__150x200.jpg',
      'path/to/images/profile_image__300x400.jpg',
    ]
  },
  'alt': 'John Doe',
}

Notice how the alias is now a combination of portrait + _ + [size]

Additional Data

Pass in any arbitary additional data to the returned dict:

image = File(user.profile_image)).additional(foo='bar', bar='baz').srcset('portrait')

{
  'urls': {
    'default': [
      'path/to/images/profile_image__150x200.jpg',
      'path/to/images/profile_image__300x400.jpg',
    ],
  },
  'foo': 'bar',
  'bar': 'baz',
  'alt': 'John Doe',
}

Notice how the alias is now a combination of portrait + _ + [size]

Adapters

Retina uses the concept of adapters. Each adapter implements a set of methods that define how an image instance (whatever it may be) should be resized. Retina ships with two adapters out of the box: FilerImageAdapter and FilerFileAdapter. This means, that if you followed the installation steps above you can pass in any django-filer File or Image model and it will output you resized versions of given file (if resizable at all).

You're free two write your own adapters and register them on the manager via the update_adapters method. Just pass in a dict where the key is a python type (like str, dict or any other object) and the value is your adapter.

Metadata

Release files for retina 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for retina 0.0.2
File Size Uploaded Provenance
retina-0.0.2.tar.gz 8.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for retina 0.0.2
File Interpreter ABI Platform
retina-0.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 16.3 kB

Release files / retina-0.0.2.tar.gz

Download URL retina-0.0.2.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ad3db7500e2336a63c7e2510b30f15c4e642d33835d2afbc250525c4da96d9ea
BLAKE2b-256 checksum
How to use checksums
1ce953e0ecc3458027b7b679a05afb37208ad6470e0178d7a80b8aa80d43639c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Release files / retina-0.0.2-py2.py3-none-any.whl

Download URL retina-0.0.2-py2.py3-none-any.whl
Size 8.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
17c26c76f7fd7873a0ad0532f0c25cdb93ef78e88841ccc933248491e005f1a0
BLAKE2b-256 checksum
How to use checksums
f7184f9616944721baed2854cd05b683f548f1919f3233a8dbfcbca1d900bbc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Security for retina 0.0.2

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.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page