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
{}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JAXB封送字符串,具有xml值,且不转义该值   java ModelMapper转换器不工作   java像HH000412或HCANN000001这样的前缀是什么意思?   验证日期输入修复java。lang.numberformatexception错误   当表具有外键时,java Telosys代码生成失败   如何使所有派生类一起只能实例化一个实例的单例抽象基类?(爪哇)   java如何在非静态服务类中使用广播接收器   java nutch爬虫相对URL问题   使用Jboss DMR下载/保存java附件   Rest模板:无法提取响应:当我们得到xml响应时,没有找到适合响应类型的HttpMessageConverter,没有绑定到JAVA对象   java如何编写可扩展窗格/面板/卡的代码   java是在ITreeViewerListener的treeExpanded()之前调用ContentProvider的getChildren()吗?   java将JComponent添加到小程序窗格   java混淆:使用简单逻辑的Flames程序