Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Django model field to store custom image crops

Project Description

django-croppy enables creating custom crops of images by specifying a name, coordinates, width and height of the crop.

django-croppy provides a custom model field responsible for creating and deleting crops. Crops are stored as serialized JSON data on the same model as the image field via django-jsonfield.

django-croppy is useful if you want to manually curate the crop size and location instead of relying on generic cropping like django-imagekit provides.

django-croppy makes use of image processors provided by django-imagekit.


First, create your model with a crop field. You can specify a custom location where to save crops to with the upload_to parameter:

from django.db import models
from croppy.fields import CropField

def upload_to(instance, filename, crop_name):
    Default function to specify a location to save crops to.

    :param instance: The model instance this crop field belongs to.
    :param filename: The image's filename this crop field operates on.
    :param crop_name: The crop name used when :attr:`CropFieldDescriptor.crop` was
    filename, ext = os.path.splitext(os.path.split(filename)[-1])
    return os.path.join('crops', u'%s-%s%s' % (filename, crop_name, ext))

class Image(models.Model):
    image = models.ImageField()
    crops = CropField('image', upload_to = upload_to)

The created crops field allows you to create, delete and inspect crops.

$ git clone
$ cd django-croppy
$ python syncdb
$ python shell
>>> from import tests
>>> image = tests.get_image('test.tiff')
>>> image
<Image: Image object>
>>> image.image
<ImageFieldFile: images/test.tiff>
>>> image.image.path

>>> # Inspect the crop data

>>> # Create a new crop called 'rect' at position 0/0
>>> # with a width of 100px and a height of 50px
>>> image.crops.create('rect', (0, 0, 100, 50))

>>> # Inspect the crop data
{'rect': {'y': 0, 'width': 100, 'height': 50, 'filename': 'crops/test-rect.tiff', 'x': 0}}

>>> # Inspect the crop
>>> image.crops.rect.path
>>> image.crops.rect.url

>>> # Save the data to database

>>> # Delete the crop
>>> image.crops.delete('rect')

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(6.4 kB) Copy SHA256 Hash SHA256
Source Jan 24, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting