Ask Your Question
1

storing opencv image in sqlite3 with python

asked 2013-08-29 02:17:30 -0600

ho_khalaf gravatar image

updated 2013-08-29 02:18:30 -0600

hi every body

how i can store image from img = cv2.imread() in sqlite3 or other databases with python and retrieve,, without saving on disk and then store in database???

i search web alot but i cant find good answer thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-29 05:04:13 -0600

berak gravatar image

updated 2013-08-29 05:13:21 -0600

after loading the image and setting up the db,

import cv2
import sqlite3

im = cv2.imread("pattern.png")
db = sqlite3.connect("my.db")
cur = db.cursor()
cur.execute("create table images(id string, img blob)")
db.commit()

you can either store the numpy array as is (wrapped in a buffer object):

cur.execute("insert into images values(?,?)",("pattern",buffer(im)))
db.commit()

or encode it as an image ( like on disk, only in memory / db ) :

_,enc = cv2.imencode(".png",im)
cur.execute("insert into images values(?,?)",("pattern",buffer(enc)))
db.commit()

use the first, if you need it for further opencv processing, the second, if you want to e.g serve it to the outside world

edit flag offensive delete link more

Comments

1

hi thank you very much but i use this and work good

i use this: cv2.imwrite( 'im.jpg' )
im = open( 'im.jpg', 'rb' ).read()

db.execute("insert into images values(?,?)",("pattern",sqlite3.Binary(im)))

and its ok

ho_khalaf gravatar imageho_khalaf ( 2013-08-30 12:17:54 -0600 )edit

learned something ;) thank you

berak gravatar imageberak ( 2013-08-30 12:52:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-08-29 02:17:30 -0600

Seen: 3,854 times

Last updated: Aug 29 '13