android - Retrieve Image from sqlite3 database and directly display on kivy window -
what getting code?
image retrieved database, created in same folder code
with open(self.filename, 'wb') output_file: output_file.write(self.ablob)
then can access image. cannot directly image database , display it.
what want get?
when click on button id: display_picture image column id: image should display image directly database without first being created in same folder.
please see image below idea of want
main.py file
from kivy.app import app import sqlite3 import os.path kivy.uix.boxlayout import boxlayout class database(boxlayout): def __init__(self,**kwargs): super(database,self).__init__(**kwargs) self.cols = 2 def on_release(self): try: self.conn = sqlite3.connect('test.db') print "done" self.sql = '''create table if not exists sample( id integer primary key autoincrement, picture blob, type text, file_name text);''' try: self.conn.execute(self.sql) print "created table" except: print "already there" except: print"fail" def insert_picture(self,conn, picture_file): open(picture_file, 'rb') input_file: ablob = input_file.read() base=os.path.basename(picture_file) afile, ext = os.path.splitext(base) sql = '''insert sample (picture, type, file_name) values(?, ?, ?);''' conn.execute(sql,[sqlite3.binary(ablob), ext, afile]) print "added picture" self.conn.commit() def on_picture_insert(self): self.picture_file = './pictures/team.jpg' self.insert_picture(self.conn,self.picture_file) def extract_picture(self,cursor, picture_id): self.sql1 = "select picture, type, file_name sample id = :id" self.param = {'id': picture_id} r in self.conn.execute(self.sql1,self.param): self.filename = r[2]+r[1] self.ablob = r[0] open(self.filename, 'wb') output_file: output_file.write(self.ablob) return self.filename def on_show_picture(self): self.cur = self.conn.cursor() self.filename = self.extract_picture(self.cur, 1) self.lista = [] self.p = self.conn.execute("select file_name,type sample") r in self.p: in r: self.lista.append(str(i)) self.ids.label_picture.text = str(self.lista) print self.ids.label_picture.text class mainapp(app): def build(self): return database() if __name__ == '__main__': mainapp().run()
main.kv file
<database>: boxlayout: orientation: 'vertical' button: size_hint: 1,.2 text: "connect databse" on_release: root.on_release() gridlayout: cols: 2 button: text: "add picture" on_release: root.on_picture_insert() button: id: display_picture text: "show picture , display names" on_release: root.on_show_picture() label: id: label_picture text: "picture name" image: id: image source: "team.jpg"
desired output after click on button (show picture , display names)
edit 1: main thing that, image source if image accessed directly database?
well can try sth : (may figure out better way)
from kivy.core.image import image coreimage kivy.uix.image import image import sqlite3 lite import io # load image db , create table images(id integer primary key, data blob); > db created ... con = lite.connect('images.db') # assume images.db sqlite3 db con: cur = con.cursor() cur.execute("select data images;") # png image extension blob_data = cur.fetchone()[0] # fetching 1 image data # load image memory , in http://kivy.org/docs/api-kivy.core.image.html#in-memory-image-loading data = io.bytesio(blob_data) im = coreimage(data, ext="png") # using im, texture image , example: class loadedimage(image): def __init__(self, **kwargs): super(loadedimage, self).__init__(**kwargs) self.texture = im.texture # using texture
Comments
Post a Comment