How to save base64 string in Python django

6,539 views
Skip to first unread message

ywaghmare5203

unread,
Jul 13, 2015, 7:40:28 AM7/13/15
to django...@googlegroups.com
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

Bill Freeman

unread,
Jul 14, 2015, 10:47:33 AM7/14/15
to django-users
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.

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

Sadaf Noor

unread,
Jul 14, 2015, 2:50:48 PM7/14/15
to django...@googlegroups.com
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. except IOError:
  12. return self.image.url


For more options, visit https://groups.google.com/d/optout.



--
 Md. Sadaf Noor (@sadaf2605)
 www.sadafnoor.com
Reply all
Reply to author
Forward
0 new messages