How to read image in blob format from a existing db

199 views
Skip to first unread message

Chris

unread,
May 18, 2021, 1:04:38 PM5/18/21
to Annet' via web2py-users
Hi, i’m trying to read images from a existing sqlite db. For some reason when i save the images to disk, they are corrupted.
this is my code:

cherry_db = DAL('sqlite://{}'.format(form.vars.cherry_file), folder=upload_folder, migrate=False)

cherry_db.define_table('image',
    Field('node_id', 'integer', requires=IS_NOT_EMPTY()),
    Field('png', 'blob')
    )

images = cherry_db(cherry_db.image.node_id == i['node_id']).select(cherry_db.image.node_id, cherry_db.image.png)

for m in images:
   name = str(uuid.uuid4())
   print('image {}'.format(name))
   print(m['png'])
   im = open('{}'.format(session.project_folder+'/'+name+'.png'), 'wb')
   im.write(m['png'])
   im.close()


Any help is appreciated

Cheers.
Chris.

--
Sent from Canary

Kevin Keller

unread,
May 18, 2021, 1:51:04 PM5/18/21
to web2py-users
I think you have to convert the image file to binary data before saving tad blob etc..?




--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/30b99d7b-941c-4387-80cd-9f521399af82%40Canary.

Chris

unread,
May 18, 2021, 2:00:08 PM5/18/21
to web...@googlegroups.com
Data is already in binary in db.



Making more test when pydal retrieve the blob data from the db is only getting just a part of the information.

When in print the rows after the select i get a small sample like this:

images = cherry_db(cherry_db.image.node_id == i['node_id']).select(cherry_db.image.node_id, cherry_db.image.png)

for m in images:
   print(m['png’])

b'<\xd1\x88\x1c4l\x04\x84\xce 0\x13\xc6\x01\r\xdd\xd0'


Cheers

--
Sent from Canary

Chris

unread,
May 18, 2021, 2:23:57 PM5/18/21
to web...@googlegroups.com
I could do it with pydal but with sqlite3 library worked perfect. Is maybe a pydal bug ?


Cheers.

--
Sent from Canary

Kevin Keller

unread,
May 18, 2021, 2:45:21 PM5/18/21
to web2py-users, py4web
Potentially. I woukd file a problem in github and use raw sql as workaround for now.

Adding the py4web community to this....





Kevin Keller

unread,
May 19, 2021, 1:34:20 PM5/19/21
to web2py-users, py4web
I filed a bug report for this on the pydal and py4web repo. 


Reply all
Reply to author
Forward
0 new messages