Re: save cropped image with PIL to database

2017-07-13 Thread 'Tom Evans' via Django users
On Mon, Jul 3, 2017 at 11:09 AM, shahab emami  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:
>
> 
> {% csrf_token %}
>
>   
>
> 
>
>   
>
>
> 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.


Re: save cropped image with PIL to database

2017-07-06 Thread Melvyn Sopacua
On Monday 03 July 2017 03:09:56 shahab emami wrote:
> hello
> 
> i have a question and i cant find answer with search.

Really?
https://duckduckgo.com/?q=django+resize+image=ffsb=
software

-- 
Melvyn Sopacua

-- 
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/15394513.8HkvbzOazS%40devstation.
For more options, visit https://groups.google.com/d/optout.


Re: save cropped image with PIL to database

2017-07-04 Thread Andréas Kühne
Sorry - can't help you there. I prefer using pip packages than writing my
own code - where possible. :-)

Regards,

Andréas

2017-07-04 10:15 GMT+02:00 shahab emami :

> but i want to do it myself.
>
> when i write code i can improve it as much as i can.
> can anybody please tell me about my mistake please
>
> On Monday, July 3, 2017 at 9:12:55 PM UTC+4:30, Andréas Kühne wrote:
>>
>> 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 :
>>
>>> 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:
>>>
>>> 
>>> {% csrf_token %}
>>>
>>>   
>>>
>>> 
>>>
>>>   
>>>
>>>
>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@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/ms
>>> gid/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+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/7be760c5-b5e3-4066-bb27-e953e829d68c%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+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/CAK4qSCciZ86du_A9K8ShTdnDNpy1W7%2BA7o_0pspLeVZcv%2BGBwg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: save cropped image with PIL to database

2017-07-04 Thread shahab emami
but i want to do it myself.

when i write code i can improve it as much as i can. 
can anybody please tell me about my mistake please

On Monday, July 3, 2017 at 9:12:55 PM UTC+4:30, Andréas Kühne wrote:
>
> 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 
> :
>
>> 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:
>>
>> 
>> {% csrf_token %}
>>
>>   
>>
>> 
>>
>>   
>>
>>
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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+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/7be760c5-b5e3-4066-bb27-e953e829d68c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: save cropped image with PIL to database

2017-07-03 Thread Andréas Kühne
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 :

> 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:
>
> 
> {% csrf_token %}
>
>   
>
> 
>
>   
>
>
> 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+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/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+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/CAK4qSCfrSH%3DWxjuqgcgasE4_Q6kxEeNGtG8oQ_jQKAF1xo23cw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


save cropped image with PIL to database

2017-07-03 Thread shahab emami
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:


{% csrf_token %}

  



  


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+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/362887c4-6b41-47e2-a9f5-f4a129b2538d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.