Hi Ricardo,
do you *really* have to save Image data in db fields?
Could you not only save a link to a generic folder holding the images and only save the path of it?

If you have to, probably binascii is your friend.
Here is a piece of code that i stole somewhere or wrote myself, i don't remember. You can encode pickable python objects eg. dicts or images.

import pickle
import cStringIO
import binascii

def encodeData(rawObj):
    """ base64 encode pickable object """
    ss=cStringIO.StringIO()
    pickle.dump(rawObj, ss)
    return binascii.b2a_base64(ss.getvalue())

def decodeData(encoded):
    """ decodes ascii base64 encoded pickled objects """
    decoded=binascii.a2b_base64(encoded)
    ss=cStringIO.StringIO()
    ss.write(decoded)
    ss.flush()
    ss.seek(0)
    return pickle.load(ss)

You have to encode your images in .save() (and obviously decode them before loading from db).
Hope this helps,

Frank


Am 29.10.2013 18:45, schrieb Ricardo Aráoz:
Hi, having some trouble saving images to db.
The file:
        self.DataStructure = (
                    ('Id', 'I', True, 'Persona', 'Id'),
                    ('Apellido', 'C', False, 'Persona', 'Apellido'),
                    ('Nombre', 'C', False, 'Persona', 'Nombre'),
                    ('DNI', ' I', False, 'Persona', 'DNI'),
                    ('Domicilio', 'C', False, 'Persona', 'Domicilio'),
                    ('Localidad', 'C', False, 'Persona', 'Localidad'),
                    ('Foto', 'L', False, 'Persona', 'Foto'),
                   )
        self.Encoding = 'utf-8'

As you can see I define Foto as a buffer ('L') field (not sure if that's how it should be done).
The control:
        foto = ImgBtn(self,
                      RegID='Foto',
                      DataSource='Persona',
                      DataField='Foto',
                      Size=(200, 200),
                      ScaleMode='Stretch',
                      DroppedFileHandler=self.Form)

Updating the image field:
        picFile = dabo.ui.getFile('png', 'jpg', 'gif', 'bmp')
        self.Foto.Value = open(picFile, "rb").read()

But when I issue self.Form.save() I get the following:

??0Meé7òÁâ¹´u?²?Ý6?9èOO·?%8?ºÒc?æÜKÂå¡?>fâIl?r1õðüi.n?×_l²?¤D!ÜâN;néõ©üA¦YYE,>c;? ±:tÛ?^ù¬ù/î?æêÐÊDVû?00p§?×4á
^bm?t¨ukàH6²~ôU?5È8ûTÜz?þU{(?±]ÏÿÙ
0
1
invalid start byte
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dFormMixin.py", line 151, in __onWxClose
    self.raiseEvent(dEvents.Close, evt)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dPemMixin.py", line 1084, in raiseEvent super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/lib/eventMixin.py", line 81, in raiseEvent
    bindingFunction(event)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dFormMixin.py", line 260, in __onClose
    if self._beforeClose(evt) == False:
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dForm.py", line 72, in _beforeClose
    ret = self.confirmChanges()
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dForm.py", line 164, in confirmChanges
    self.save(dataSource=biz.DataSource)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/ui/uiwx/dForm.py", line 414, in save
    bizobj.saveAll(saveTheChildren=self.SaveChildren)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/biz/dBizobj.py", line 493, in saveAll
    saveTheChildren=saveTheChildren, scanRequeryChildren=False)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/biz/dBizobj.py", line 969, in scanKeys
    ret = func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/biz/dBizobj.py", line 560, in save
    cursor.save(includeNewUnchanged=True)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/db/dCursorMixin.py", line 1611, in save
    saverow(row)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/db/dCursorMixin.py", line 1575, in saverow
    self.__saverow(row)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/db/dCursorMixin.py", line 1686, in __saverow
    res = aux.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/db/dCursorMixin.py", line 363, in execute
    self._dblogExecute("execute() FAILED", sql, params)
File "/usr/local/lib/python2.7/dist-packages/Dabo-0.9.13-py2.7.egg/dabo/db/dCursorMixin.py", line 328, in _dblogExecute
    logmsg = "%s SQL: %s, PARAMS: %s" % (msg, sql, params)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

It would seem the problem is the encoding? Dabo is trying to decode the image? Don't know if I'm making some kind of mistake or if it's some bug, and I don't know if this can be worked out. But if someone has persisted images in a db I would appreciate any workaround you can suggest.
Thanks



--- StripMime Report -- processed MIME parts ---
multipart/alternative
 text/plain (text body -- kept)
 text/html
---
_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/526ff442.10...@gmail.com



_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/5270fc42.3020...@stuess.de

Reply via email to