Monday, July 3, 2017

Re: save cropped image with PIL to database

I would have a look at the easy-thumbnails plugin (http://easy-thumbnails.readthedocs.io/en/2.1/). It uses pillow behind the scenes and it is very easy to do the cropping and everything that you need and you don't need to write it yourself :-)

Regards,

Andréas

2017-07-03 12:09 GMT+02:00 shahab emami <royaramezan@gmail.com>:
hello

i have a question and i cant find answer with search.

i want to save cropped image in database then :
this is my model:

class Photo(models.Model):
    file        
= models.ImageField(upload_to='%Y/%m/%d', blank=True,null=True)

i have a form then user can select image and send to view like this:
<form method="post" enctype="multipart/form-data" " >
    {% csrf_token %}

 
<input type="file"  id="photo_id" name="file">

   
<input type="submit" value=" send image" />

 
</form>

and in the view i have this :
from PIL import Image


def save_photo(request):

    _photo
, created = models.Photo.objects.get_or_create(pk=1)

   
if request.method == 'POST':
        photo
= request.FILES.get('file')

        image
= Image.open(photo)

        cropped_image
= image.crop((100, 100, 200, 200))
        resized_image
= cropped_image.resize((200, 200), Image.ANTIALIAS)
        _photo
.file = resized_image
        _photo
.save()


        return HttpResponse("your image has been saved")




but i get this error:

'Image' object has no attribute '_committed'


i'm sure my mistake is in views.py but i dont now how to fix it. how can put resized_image that is an object of Image class to 
a model.
can you help me please?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/362887c4-6b41-47e2-a9f5-f4a129b2538d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCfrSH%3DWxjuqgcgasE4_Q6kxEeNGtG8oQ_jQKAF1xo23cw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment