Skip to main content

Easy thumbnails for Django

Project description

The powerful, yet easy to implement thumbnailing application for Django.

To install this application into your project, just add it to your INSTALLED_APPS setting (and run manage.py syncdb):

INSTALLED_APPS = (
    ...
    'easy_thumbnails',
)

Template usage

To generate thumbnails in your template, use the {% thumbnail %} tag. To make this tag available for use in your template, use:

{% load thumbnails %}

Basic tag Syntax:

{% thumbnail [source] [size] [options] %}

source must be a File object, usually an Image/FileField of a model instance.

size can either be:

  • the size in the format [width]x[height] (for example, {% thumbnail person.photo 100x50 %}) or

  • a variable containing a valid size (i.e. either a string in the [width]x[height] format or a tuple containing two integers): {% thumbnail person.photo size_var %}.

options are a space separated list of options which are used when processing the image to a thumbnail such as sharpen, crop and quality=90.

Model usage

You can use the ThumbnailerField or ThumbnailerImageField fields (based on FileField and ImageField, respectively) for easier access to retrieve (or generate) thumbnail images.

By passing a resize_source argument to the ThumbnailerImageField, you can resize the source image before it is saved:

class Profile(models.Model):
    user = models.ForeignKey('auth.User')
    avatar = ThumbnailerImageField(
        upload_to='avatars',
        resize_source=dict(size=(50, 50), crop='smart'),
    )

Lower level usage

Thumbnails are generated with a Thumbnailer instance. For example:

from easy_thumbnails import Thumbnailer

def square_thumbnail(source):
    thumbnail_options = dict(size=(100, 100), crop=True, bw=True)
    return Thumbnailer(source).get_thumbnail(thumbnail_options)

By default, get_thumbnail saves the file (using file storage). The source file used to instanciate the Thumbnailer must have a name instance relative to the storage root.

The ThumbnailFile object provided makes this easy:

from easy_thumbnails import ThumbnailFile

# For an existing file in storage:
source = ThumbnailFile('animals/aarvark.jpg')
square_thumbnail(source)

# For a new file:
picture = open('/home/zookeeper/pictures/my_anteater.jpg')
source = ThumbnailFile('animals/anteater.jpg', file=picture)
square_thumbnail(source)

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

easy-thumbnails-1.0-alpha-2.tar.gz (20.8 kB view details)

Uploaded Source

File details

Details for the file easy-thumbnails-1.0-alpha-2.tar.gz.

File metadata

File hashes

Hashes for easy-thumbnails-1.0-alpha-2.tar.gz
Algorithm Hash digest
SHA256 c6bf81ca2860f0eaa1ba2149c32d03262c5828e23d98d1f8f8bcdb38a0d7ea2d
MD5 ad2d0ab041e37dafe2399ca0a7697406
BLAKE2b-256 b5e08bdb35ac5996ff597b2802f2a9b9cd161d35819e93b6923fafa30ef5842b

See more details on using hashes here.

Provenance

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