If you can avoid saving images to database then the solution would look
like following where you are serving base64 url of image to reduce your
website data bandwidth:


   1. from django.db import models
   2. class Photo( models.Model ):
   3. title = models.CharField( max_length=255 )
   4. image = models.ImageField( upload_to="photos/", max_length=255)
   5. @property
   6. def image_url( self ):
   7. try:
   8. img = open( self.image.path, "rb")
   9. data = img.read()
   10. return "data:image/jpg;base64,%s" % data.encode('base64')
   11.
   12. except IOError:
   13. return self.image.url


2015-07-14 20:46 GMT+06:00 Bill Freeman <[email protected]>:

> You don't show where the 'Image' object comes from (in Image.open), so I
> can't be specific, but here are some generalities:
>
> "Incorrect padding" is probably a message from the base 64 decoder.
> Capture your request.POST[photo] to play with separately.  If you are doing
> this under the development server you can add a pdb.set_trace() to get a
> prompt where you can play with things.  Alternatively, put the decode and
> the StringIO creation in separate statements so the the line generating the
> error will be clear.
>
> Are you sure that the image is base 64?  If it's coming from a file field
> in an HTML form, it probably isn't, which would explain the decode error
> (it I'm correct and it is a decoder error).
>
> It is customary to store images on the file system, putting only the path
> to the file in the database, as part of a model instance. That way the
> front end server (Apache or nginx, etc.) can serve the images without
> involving python and the database..  There are image oriented model fields
> available in django to facilitate this, see the excellent documentation.
>
> If, for whatever reason, you must store the image in the database, you can
> store it as a (binary) string.  If your image object makes the data
> available as a string, there will be no need to pump it through a StringIO
> object.
>
> On Mon, Jul 13, 2015 at 7:40 AM, ywaghmare5203 <[email protected]>
> wrote:
>
>> Hello all,
>>
>> I am trying to save base64 string string image to database but I am not
>> able to store. I am beginner for python django language.
>>
>> I am following these steps:--
>>
>> from base64 import b64decode
>> from django.core.files.base import ContentFile
>> from time import time
>> import cStringIO
>> import base64
>>
>>
>> pic = cStringIO.StringIO()
>>         image_string =
>> cStringIO.StringIO(base64.b64decode(request.POST['photo']))
>>         image = Image.open(image_string)
>>         image.save(pic, image.format, quality = 100)
>>         pic.seek(0)
>>         return HttpResponse(pic, content_type='image/jpeg')
>>
>> but I have error
>>
>> TypeError: Incorrect padding
>>
>> Please help me for store the base64 image string in the database.
>>
>>
>> Thanks & Regards,
>>
>> Yogesh Waghnare
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/54b12c7e-e9b0-4a47-85ce-988c897bbd11%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/54b12c7e-e9b0-4a47-85ce-988c897bbd11%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAB%2BAj0v_qnReqeKyVib48_8qRGwo9Qs4LY%2BVn1iNX5DjAbVCcQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAB%2BAj0v_qnReqeKyVib48_8qRGwo9Qs4LY%2BVn1iNX5DjAbVCcQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
  Md. Sadaf Noor (@sadaf2605 <https://twitter.com/sadaf2605>)
 www.sadafnoor.com

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAJ2eVqFzYNhx9FyMW2i%2BATc%3DZ0MWHj%2Bhxb2DwmF%3DpD%3DaXa60w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to