Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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()
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()

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

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()
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

you can either store after loading the numpy array as is (wrapped in a buffer object):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