Django app for image crop using a cropping tool
Asked Answered
K

1

7

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

Kippy answered 26/10, 2011 at 19:27 Comment(1)
Concerning django-image-cropping: If you look at the ImageCropField (github.com/jonasundderwolf/django-image-cropping/blob/master/…) you'll notice that it basically takes the standard image field and only adds the widget for the admin. And the ImageRatioField stores the coordinates as comma separated strings, so it's not too different from pastylegs' suggestion. So you should be able to easily redeclare a standard image field in an existing app as ImageCropField. Or what are your concerns ("very coupled")?Yorker
M
17

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 - https://mcmap.net/q/116169/-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
    ...
Margarito answered 26/10, 2011 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.