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 <ywaghmare5...@gmail.com>
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 django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> 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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to