Skip to main content

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.

Usage

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, image, crop_name):
    filename, ext = os.path.splitext(os.path.split(image.name)[-1])
    return os.path.join('crops', '{}-{}{}'.format(filename, crop_name, ext))

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

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

$ cd tests && source test.sh && django-admin.py syncdb --settings tests.settings
...
$ django-admin.py shell --settings tests.settings
...
>>> from tests.app import tests
>>> image = tests.get_image('test.tiff')
>>> image
<Image: Image object>
>>> image.image
<ImageFieldFile: images/test.tiff>
>>> image.image.path
u'/home/alen/projects/django-croppy/tests/test-media/images/test.tiff'

>>> # Inspect the crop data
>>> image.crops.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
>>> image.crops.data
{'rect': {'y': 0, 'width': 100, 'height': 50, 'filename': 'crops/test-rect.tiff', 'x': 0}}

>>> # Inspect the crop
>>> image.crops.rect.name
'crops/test-rect.tiff'
>>> image.crops.rect.path
u'/home/alen/projects/django-croppy/tests/test-media/crops/test-rect.tiff'
>>> image.crops.rect.url
'/test-media/crops/test-rect.tiff'

>>> # Save the data to database
>>> image.save()

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

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

django-croppy-0.0.6.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file django-croppy-0.0.6.tar.gz.

File metadata

  • Download URL: django-croppy-0.0.6.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-croppy-0.0.6.tar.gz
Algorithm Hash digest
SHA256 eac61c88e8689bca68a4141ad77f25389c22ae91deff2544445a72abcaf0e7ee
MD5 c3ea413be53683443be1486050cf3f64
BLAKE2b-256 dadbdedd7ae3db971e0af1f7f2f1a159ffb9f7b0f4002f9682aa8357a9739dcf

See more details on using hashes here.

Supported by

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