On Mon, Jul 3, 2017 at 11:09 AM, shahab emami <royarame...@gmail.com> wrote: > 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')
This is a django.core.files.File (or subclass) object. It is file-like and can be treated as a file. > > image = Image.open(photo) > > cropped_image = image.crop((100, 100, 200, 200)) > resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) resized_image is a pil.Image. Again, it is file-like, but it is not a django.core.files.File... > _photo.file = resized_image > _photo.save() so this doesn't work. You will need to wrap the PIL.Image file-like object with the django File class (remember appropriate imports): _photo.file = File(resized_image) _photo.save() Cheers Tom -- 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+unsubscr...@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/CAFHbX1%2BTeg-joCvt%3DosHTThpzduQ0_ZYr%2BKvMfbD%3DJJPHZ2gcw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.