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第三方库


热门话题
SpringBoot如何在Java中创建固定长度的平面文件?   java是freemarker类型。果心无法解决此问题。它是从required间接引用的。类文件   设置字符串格式时发生java FormatFlagsTransversionMismatchException异常   无法将java应用程序连接到sql server 2008 R2   在关闭事件之前,使用Java API BMC补救方法更新“分类”   java如何在使用opencsv写入csv时保持空值   java在spring中是否需要事务属性?   java Hibernate抑制信息消息   java如何在spring data cosmos的@Container中给出布尔值?   java获取旋转矩形的角   如何在java中获得对象的两个数组列表的差异   java与新接口相比,嵌套模板的优点和缺点是什么?   Java Apache Derby十进制列数据为0.001输出为0.0   用于隐藏包含特定单词的元素的javascript用户脚本   java Jackson objectMapper无法读取UTF8   java在JavaFX中创建太多事件处理程序   如何在运行Redhat Linux的amazon ec2微实例上卸载openjdk   如何使用Java swing创建一个动态列表,其中必须为每个元素指定多个参数   java如何在将面板放置在gridlayout上时消除面板之间的间隙?   java标准方法名称,为什么println中的l不是大写?