使用裁剪工具进行图像裁剪的Django应用程序 [英] Django app for image crop using a cropping tool

查看:304
本文介绍了使用裁剪工具进行图像裁剪的Django应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我发现这个工具: p>



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



我们正在处理一个具有许多功能的大型应用程序,非常困难地改变了写入的逻辑。

解决方案

认为这是你会有的事情最好是写自己,因为这取决于你的数据和模型是如何(如果你想要保存原始文件),你是否(以及在哪里)保存作物,即使你有一个大的应用程序,你会可能花费更多的时间试图弯曲其他代码来做你所需要的情况。



(这段代码非常粗糙 - 我只是布置步骤真的)



如果你有一个带有imagefield的模型,您可以添加第二个图像字段来保存裁剪后的图像:

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

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

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

形式:

  class MyModelForm(form。 
隐藏一个字段来保存用户选择的坐标
crop_coords = forms.CharField(attrs = {'style':'display:none'})

class Meta:
model = MyModel

这个:

  def some_view(request):
form = request.POST
if form.is_valid )
crop_coords = form.cleaned_data ['crop_coords']
#使用simpleson解码协调(或者您通过它们)
...
#创建裁剪图像
original_image = form.cleaned_data ['image']
cropped_image = cropper(original_image.path,crop_coords)
...
#将其保存回db - http://stackoverflow.com/questions / 1308386 / programmatically-saving-image-to-django-imagefield
...

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

 #看这里:http://djangosnippets.org/snippets/224 / 
def cropper(original_image_path,crop_coords):
打开原始图像,创建并返回一个新的裁剪图像
...
pre>

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

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?

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

解决方案

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:

{ '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://stackoverflow.com/questions/1308386/programmatically-saving-image-to-django-imagefield
        ...

and a function to create the cropped image using PIL:

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

这篇关于使用裁剪工具进行图像裁剪的Django应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆