Automated image processing for Django. Currently v4.0
Find a file
Bryan Veloso 0656292bef Merge branch 'release/1.1.0'
* release/1.1.0: (28 commits)
  Adding (crude) changelog information for 1.1.0.
  Bumping the version number.
  Removing Pillow from requirements.txt and adding a note about PIL/Pillow to the README. References #72.
  Being a bit friendlier to Python 2.5 users... or is it 2.4. Fixes #82.
  Change handling of spec deletion
  No need to check for None
  Catch KeyError on image.info dictionary
  Using difference instead of subtract
  Indentation tweak
  Makes evaluation of `source_file` lazy
  Test illustrating #75, #74
  Separate create_photo method
  avoid Transpose to crash when exif data doesn't exists
  Allows `None` value for `processors` argument
  Remove _Resize class
  Adds crop module to docs
  Explicitly import crop module
  Renames processor to `TrimBorderColor`
  Gives precedence to user options
  Replaces `quality` argument with `options` dict
  ...
2011-12-22 22:18:31 -08:00
docs Adding (crude) changelog information for 1.1.0. 2011-12-22 22:14:32 -08:00
imagekit Bumping the version number. 2011-12-22 22:14:14 -08:00
tests Being a bit friendlier to Python 2.5 users... or is it 2.4. Fixes #82. 2011-12-22 21:48:15 -08:00
.gitignore Adding distutils stuff to .gitignore. 2011-02-11 12:59:24 -08:00
AUTHORS Moving Matt to maintainers. 2011-11-11 16:20:13 +09:00
LICENSE Wrangling in a stray line. 2011-02-10 15:20:40 -08:00
MANIFEST.in Making sure the templates make it via pip. Fixes #39. 2011-11-02 13:42:35 +09:00
README.rst Removing Pillow from requirements.txt and adding a note about PIL/Pillow to the README. References #72. 2011-12-22 21:58:42 -08:00
requirements.txt Removing Pillow from requirements.txt and adding a note about PIL/Pillow to the README. References #72. 2011-12-22 21:58:42 -08:00
setup.py Making sure the templates make it via pip. Fixes #39. 2011-11-02 13:42:35 +09:00

ImageKit is a Django app that helps you to add variations of uploaded images
to your models. These variations are called "specs" and can include things
like different sizes (e.g. thumbnails) and black and white versions.


Installation
------------

1. Install `PIL`_ or `Pillow`_. If you're using `ImageField`s in Django, you
   should have already done this.
2. ``pip install django-imagekit``
   (or clone the source and put the imagekit module on your path)
3. Add ``'imagekit'`` to your ``INSTALLED_APPS`` list in your project's settings.py

.. note:: If you've never seen Pillow before, it considers itself a
   more-frequently updated "friendly" fork of PIL that's compatible with
   setuptools. As such, it shares the same namespace as PIL does and is a
   drop-in replacement.

.. _`PIL`: http://pypi.python.org/pypi/PIL
.. _`Pillow`: http://pypi.python.org/pypi/Pillow


Adding Specs to a Model
-----------------------

Much like ``django.db.models.ImageField``, Specs are defined as properties
of a model class::

    from django.db import models
    from imagekit.models import ImageSpec

    class Photo(models.Model):
        original_image = models.ImageField(upload_to='photos')
        formatted_image = ImageSpec(image_field='original_image', format='JPEG',
                options={'quality': 90})

Accessing the spec through a model instance will create the image and return
an ImageFile-like object (just like with a normal
``django.db.models.ImageField``)::

    photo = Photo.objects.all()[0]
    photo.original_image.url # > '/media/photos/birthday.tiff'
    photo.formatted_image.url # > '/media/cache/photos/birthday_formatted_image.jpeg'

Check out ``imagekit.models.ImageSpec`` for more information.


Processors
----------

The real power of ImageKit comes from processors. Processors take an image, do
something to it, and return the result. By providing a list of processors to
your spec, you can expose different versions of the original image::

    from django.db import models
    from imagekit.models import ImageSpec
    from imagekit.processors import resize, Adjust

    class Photo(models.Model):
        original_image = models.ImageField(upload_to='photos')
        thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1),
                resize.Crop(50, 50)], image_field='original_image',
                format='JPEG', options={'quality': 90})

The ``thumbnail`` property will now return a cropped image::

    photo = Photo.objects.all()[0]
    photo.thumbnail.url # > '/media/cache/photos/birthday_thumbnail.jpeg'
    photo.thumbnail.width # > 50
    photo.original_image.width # > 1000

The original image is not modified; ``thumbnail`` is a new file that is the
result of running the ``imagekit.processors.resize.Crop`` processor on the
original.

The ``imagekit.processors`` module contains processors for many common
image manipulations, like resizing, rotating, and color adjustments. However,
if they aren't up to the task, you can create your own. All you have to do is
implement a ``process()`` method::

    class Watermark(object):
        def process(self, image):
            # Code for adding the watermark goes here.
            return image

    class Photo(models.Model):
        original_image = models.ImageField(upload_to='photos')
        watermarked_image = ImageSpec([Watermark()], image_field='original_image',
                format='JPEG', options={'quality': 90})


Admin
-----

ImageKit also contains a class named ``imagekit.admin.AdminThumbnail``
for displaying specs (or even regular ImageFields) in the
`Django admin change list`_. AdminThumbnail is used as a property on
Django admin classes::

    from django.contrib import admin
    from imagekit.admin import AdminThumbnail
    from .models import Photo


    class PhotoAdmin(admin.ModelAdmin):
        list_display = ('__str__', 'admin_thumbnail')
        admin_thumbnail = AdminThumbnail(image_field='thumbnail')


    admin.site.register(Photo, PhotoAdmin)

AdminThumbnail can even use a custom template. For more information, see
``imagekit.admin.AdminThumbnail``.

.. _`Django admin change list`: https://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-change-list