Django模型字段存储自定义图像作物
django-cropp的Python项目详细描述
django-croppy通过指定 作物的名称、坐标、宽度和高度。
django-croppy提供负责创建 以及删除作物。作物作为序列化的json数据存储在 模型作为图像场通过 django-jsonfield。
django-croppy如果要手动调整作物大小,则非常有用 和地点而不是像 django-imagekit 提供。
django-croppy使用 django-imagekit。
用法
首先,使用裁剪区域创建模型。可以指定自定义 使用upload_to参数保存作物的位置:
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 called. """ 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)
创建的crops字段允许您创建、删除和检查 作物。
$ git clone git@github.com:caffeinehit/django-croppy.git $ cd django-croppy $ python manage.py syncdb ... $ python manage.py shell ... >>> 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 {}