Skip to main content

Painless retina image handling

Project description

👁 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retina-0.0.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

retina-0.0.2-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file retina-0.0.2.tar.gz.

File metadata

  • Download URL: retina-0.0.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for retina-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ad3db7500e2336a63c7e2510b30f15c4e642d33835d2afbc250525c4da96d9ea
MD5 711bd9994701180cc2f06dd925850c96
BLAKE2b-256 1ce953e0ecc3458027b7b679a05afb37208ad6470e0178d7a80b8aa80d43639c

See more details on using hashes here.

File details

Details for the file retina-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: retina-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for retina-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 17c26c76f7fd7873a0ad0532f0c25cdb93ef78e88841ccc933248491e005f1a0
MD5 90c711f8e2683b3fb578dcba415a97f9
BLAKE2b-256 f7184f9616944721baed2854cd05b683f548f1919f3233a8dbfcbca1d900bbc5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page