Automated image processing for Django. Currently v4.0
Find a file
Bryan Veloso 098425a383 Merge branch 'release/0.3.6'
* release/0.3.6:
  Bumping the verison numbers.
  Whitespacing.
  Adding Jonathan Slenders to AUTHORS (thanks for spotting that signals bug).
  fixed 'sender' not defined bug (2)
  fixed 'sender' not defined bug
  Instead of overriding Model.delete(), we'll connect a public clear_cache() method to a post_delete() signal so that bulk actions clean props as well.
2011-04-01 11:59:04 -07:00
imagekit Bumping the verison numbers. 2011-04-01 11:56:44 -07:00
.gitignore Adding distutils stuff to .gitignore. 2011-02-11 12:59:24 -08:00
AUTHORS Adding Jonathan Slenders to AUTHORS (thanks for spotting that signals bug). 2011-03-26 00:42:37 -07:00
LICENSE Wrangling in a stray line. 2011-02-10 15:20:40 -08:00
MANIFEST.in Fleshing out setup.py. Adding MANIFEST.in. 2011-02-11 01:27:10 -08:00
README.rst I suck horribly at checking my work. 2011-02-10 15:15:52 -08:00
setup.py Bumping the verison numbers. 2011-04-01 11:56:44 -07:00

===============
django-imagekit
===============

ImageKit In 7 Steps
===================

Step 1
******

::

    $ pip install django-imagekit

(or clone the source and put the imagekit module on your path)

Step 2
******

Add ImageKit to your models.

::

    # myapp/models.py

    from django.db import models
    from imagekit.models import ImageModel

    class Photo(ImageModel):
        name = models.CharField(max_length=100)
        original_image = models.ImageField(upload_to='photos')
        num_views = models.PositiveIntegerField(editable=False, default=0)

        class IKOptions:
            # This inner class is where we define the ImageKit options for the model
            spec_module = 'myapp.specs'
            cache_dir = 'photos'
            image_field = 'original_image'
            save_count_as = 'num_views'

Step 3
******

Create your specifications.

::

    # myapp/specs.py

    from imagekit.specs import ImageSpec
    from imagekit import processors

    # first we define our thumbnail resize processor
    class ResizeThumb(processors.Resize):
        width = 100
        height = 75
        crop = True

    # now we define a display size resize processor
    class ResizeDisplay(processors.Resize):
        width = 600

    # now let's create an adjustment processor to enhance the image at small sizes
    class EnchanceThumb(processors.Adjustment):
        contrast = 1.2
        sharpness = 1.1

    # now we can define our thumbnail spec
    class Thumbnail(ImageSpec):
        access_as = 'thumbnail_image'
        pre_cache = True
        processors = [ResizeThumb, EnchanceThumb]

    # and our display spec
    class Display(ImageSpec):
        increment_count = True
        processors = [ResizeDisplay]

Step 4
******

Flush the cache and pre-generate thumbnails (ImageKit has to be added to ``INSTALLED_APPS`` for management command to work).

::

    $ python manage.py ikflush myapp

Step 5
******

Use your new model in templates.

::

    <div class="original">
    <img src="{{ photo.original_image.url }}" alt="{{ photo.name }}">
    </div>

    <div class="display">
    <img src="{{ photo.display.url }}" alt="{{ photo.name }}">
    </div>

    <div class="thumbs">
    {% for p in photos %}
    <img src="{{ p.thumbnail_image.url }}" alt="{{ p.name }}">
    {% endfor %}
    </div>

Step 6
******

Play with the API.

::

    >>> from myapp.models import Photo
    >>> p = Photo.objects.all()[0]
    <Photo: MyPhoto>
    >>> p.display.url
    u'/static/photos/myphoto_display.jpg'
    >>> p.display.width
    600
    >>> p.display.height
    420
    >>> p.display.image
    <JpegImagePlugin.JpegImageFile instance at 0xf18990>
    >>> p.display.file
    <File: /path/to/media/photos/myphoto_display.jpg>
    >>> p.display.spec
    <class 'myapp.specs.Display'>

Step 7
******

Enjoy a nice beverage.

::

    from refrigerator import beer

    beer.enjoy()