Django app使用裁剪工具进行图像裁剪

时间:2022-06-07 21:21:27

I need an app for crop an image in the client side, I mean, using a cropping tool like Jcrop jquery plugin.

我需要一个用于在客户端裁剪图像的应用程序,我的意思是,使用像Jcrop jquery插件这样的裁剪工具。

I found this tools:

我找到了这个工具:

But the last two depends of admin and the two first seem very coupled to ther own ImageFields and models, any good solution?

但最后两个取决于管理员和两个首先似乎非常耦合自己的ImageFields和模型,任何好的解决方案?

We are working over a big application with many features and is very difficult change the logic writed

我们正在研究一个具有许多功能的大型应用程序,并且很难改变逻辑写入

1 个解决方案

#1


16  

I think this is something that you will probably be best off writing yourself as it depends on how your data and models are layed out, whether (and where) you want to save the crops, if you want to keep the originals etc. Even if you have a big app, you will probably spend more time trying to bend other code to do what you need in your situation.

我认为这可能是你最好自己写的,因为它取决于你的数据和模型的布局,是否(以及在哪里)你想保存庄稼,如果你想保留原件等等。即使你有一个很棒的应用程序,你可能会花更多的时间来尝试弯曲其他代码,以便在你的情况下做你需要的。

(This code is very rough - I'm just laying out the steps really)

(这段代码非常粗糙 - 我只是简单地布置了这些步骤)

If you have a model with an imagefield, you could add a second image field to hold the cropped image:

如果您的模型包含图像域,则可以添加第二个图像字段来保存裁剪后的图像:

class MyModel(models.Model):
    image = models.ImageField(...)
    image_crop = models.ImageField(...)

and a form with an extra field to hold the jcrop coordinates that will be populated in the form on the client side (the field will be hidden). In what form you save the coordinates into the field is up to you, but it might be an idea to use a json dictionary (json.js on the client side and simplejson on the server side), something like:

和一个带有额外字段的表单,用于保存将在客户端表单中填充的jcrop坐标(该字段将被隐藏)。以什么形式将坐标保存到字段中取决于您,但使用json字典(客户端的json.js和服务器端的simplejson)可能是个主意,例如:

{ 'x1' : '145', 'y1' : '200'  ... }

the form:

表格:

class MyModelForm(form.ModelForm):
    """ Hide a field to hold the coordinates chosen by the user """
    crop_coords = forms.CharField(attrs={'style':'display:none'})        

    class Meta:
         model = MyModel

a view that processes all this:

处理所有这些的视图:

def some_view(request):
    form = request.POST
    if form.is_valid():
        crop_coords = form.cleaned_data['crop_coords']
        # decode the coords using simpleson (or however you passed them)
        ...
        # create a cropped image 
        original_image = form.cleaned_data['image']
        cropped_image = cropper(original_image.path, crop_coords)
        ...
        # save it back to the db - http://*.com/questions/1308386/programmatically-saving-image-to-django-imagefield
        ...

and a function to create the cropped image using PIL:

以及使用PIL创建裁剪图像的功能:

# Look here: http://djangosnippets.org/snippets/224/
def cropper(original_image_path, crop_coords):
    """ Open original, create and return a new cropped image
    ...

#1


16  

I think this is something that you will probably be best off writing yourself as it depends on how your data and models are layed out, whether (and where) you want to save the crops, if you want to keep the originals etc. Even if you have a big app, you will probably spend more time trying to bend other code to do what you need in your situation.

我认为这可能是你最好自己写的,因为它取决于你的数据和模型的布局,是否(以及在哪里)你想保存庄稼,如果你想保留原件等等。即使你有一个很棒的应用程序,你可能会花更多的时间来尝试弯曲其他代码,以便在你的情况下做你需要的。

(This code is very rough - I'm just laying out the steps really)

(这段代码非常粗糙 - 我只是简单地布置了这些步骤)

If you have a model with an imagefield, you could add a second image field to hold the cropped image:

如果您的模型包含图像域,则可以添加第二个图像字段来保存裁剪后的图像:

class MyModel(models.Model):
    image = models.ImageField(...)
    image_crop = models.ImageField(...)

and a form with an extra field to hold the jcrop coordinates that will be populated in the form on the client side (the field will be hidden). In what form you save the coordinates into the field is up to you, but it might be an idea to use a json dictionary (json.js on the client side and simplejson on the server side), something like:

和一个带有额外字段的表单,用于保存将在客户端表单中填充的jcrop坐标(该字段将被隐藏)。以什么形式将坐标保存到字段中取决于您,但使用json字典(客户端的json.js和服务器端的simplejson)可能是个主意,例如:

{ 'x1' : '145', 'y1' : '200'  ... }

the form:

表格:

class MyModelForm(form.ModelForm):
    """ Hide a field to hold the coordinates chosen by the user """
    crop_coords = forms.CharField(attrs={'style':'display:none'})        

    class Meta:
         model = MyModel

a view that processes all this:

处理所有这些的视图:

def some_view(request):
    form = request.POST
    if form.is_valid():
        crop_coords = form.cleaned_data['crop_coords']
        # decode the coords using simpleson (or however you passed them)
        ...
        # create a cropped image 
        original_image = form.cleaned_data['image']
        cropped_image = cropper(original_image.path, crop_coords)
        ...
        # save it back to the db - http://*.com/questions/1308386/programmatically-saving-image-to-django-imagefield
        ...

and a function to create the cropped image using PIL:

以及使用PIL创建裁剪图像的功能:

# Look here: http://djangosnippets.org/snippets/224/
def cropper(original_image_path, crop_coords):
    """ Open original, create and return a new cropped image
    ...