我想从数据库 sqlite3 中检索一个 blob 并将其转换为图像
I want to retrieve a blob from database sqlite3 and convert it into image
我得到了这张 Treeview
图片 table 而不是图片
global img, filename
self.fob = open(filename, 'rb')
self.fob = self.fob.read()
entities = (self.makecb.get(), self.modelcb.get(),self.Yearcb.get(), self.Transmissioncb.get(),self.Fuelcb.get(),self.colorcb.get(),self.Enginedisplacementcb.get(),self.PreviousownersE.get(), self.Vehicleorigincb.get(),self.mileagecb.get(), self.numofpasscb.get(),self.lincesplatenum.get().replace(" ", ""), self.fob,
self.sunroofcheck, self.leatherseatcheck,self.sensorcheck, self.cameracheck, self.AlloyWheelscheck,
self.centrallockcheck, self.monitorcheck, self.alarmsystemcheck, self.Daylightledcheck, self.Airbagcheck,
self.Seller_name.get(), self.cashepayments.get(), self.price.get(), self.lincses_start_date_E.get(),
self.linces_expiredate_E.get(), self.insurancecomp_E.get(), self.insurancecomtype_cb.get())
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.cursorObj.execute(
'''INSERT INTO Vechicle_info(carmake, carmodel, caryear, cartransmition, carfuel, carcolor, carengine, carpreviousowners, carorigin, carmileage, carnumofpassengers, carlincesplatenum, image, Sunroof, leatherseat, sensor, camera, AlloyWheels, centrallock, monitor, alarmsystem, Daylightled, Airbag, sellerrname, paymentmethod, price, Licensesdatestart, Licensedateexpire, insurancecompanyname, insurancetype) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
entities)
self.con.commit()
self.cursorObj.close()
问题:
我想使用其他值从 sqllite3 数据库中检索 blob,并使用 tkinter 将其插入到 treeview table 中。当将这些值插入 table 时,我在图像列 \xfa\xf4\xe9\xd0\xd2\xd2\xc2\x8c\x193\xa0\xab\xab\xcb\x96Q\xcaf\ 上得到了这些类型的二进制文件,所以如何从数据库中获取图像,然后将其转换为图像并将该图像插入树视图 table tkinter.
如果存储到数据库中的图像是原始数据,如下例:
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
# sample code to insert record to database
cursor.execute('CREATE TABLE IF NOT EXISTS sample (image BLOB, name TEXT)')
with open('sample.png', 'rb') as f:
data = f.read()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (data, 'Sample'))
cnx.commit()
cursor.close()
cnx.close()
然后你可以使用ImageTk.PhotoImage(data=...)
来创建要在tkinter应用程序中使用的图像。
下面是一个例子:
import sqlite3
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
root = tk.Tk()
s = ttk.Style()
s.configure('Treeview', rowheight=100) # because the image is around 100x100
tree = ttk.Treeview(root, columns=['Name'], height=2)
tree.pack()
tree.heading('#0', text='Image')
tree.heading('Name', text='Name')
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
cursor.execute('SELECT * FROM sample')
imglist = []
for rec in cursor:
img = ImageTk.PhotoImage(data=rec[0])
tree.insert('', 'end', image=img, values=rec[1:])
imglist.append(img)
cursor.close()
cnx.close()
root.mainloop()
结果:
我得到了这张 Treeview
图片 table 而不是图片
global img, filename
self.fob = open(filename, 'rb')
self.fob = self.fob.read()
entities = (self.makecb.get(), self.modelcb.get(),self.Yearcb.get(), self.Transmissioncb.get(),self.Fuelcb.get(),self.colorcb.get(),self.Enginedisplacementcb.get(),self.PreviousownersE.get(), self.Vehicleorigincb.get(),self.mileagecb.get(), self.numofpasscb.get(),self.lincesplatenum.get().replace(" ", ""), self.fob,
self.sunroofcheck, self.leatherseatcheck,self.sensorcheck, self.cameracheck, self.AlloyWheelscheck,
self.centrallockcheck, self.monitorcheck, self.alarmsystemcheck, self.Daylightledcheck, self.Airbagcheck,
self.Seller_name.get(), self.cashepayments.get(), self.price.get(), self.lincses_start_date_E.get(),
self.linces_expiredate_E.get(), self.insurancecomp_E.get(), self.insurancecomtype_cb.get())
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.cursorObj.execute(
'''INSERT INTO Vechicle_info(carmake, carmodel, caryear, cartransmition, carfuel, carcolor, carengine, carpreviousowners, carorigin, carmileage, carnumofpassengers, carlincesplatenum, image, Sunroof, leatherseat, sensor, camera, AlloyWheels, centrallock, monitor, alarmsystem, Daylightled, Airbag, sellerrname, paymentmethod, price, Licensesdatestart, Licensedateexpire, insurancecompanyname, insurancetype) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
entities)
self.con.commit()
self.cursorObj.close()
问题: 我想使用其他值从 sqllite3 数据库中检索 blob,并使用 tkinter 将其插入到 treeview table 中。当将这些值插入 table 时,我在图像列 \xfa\xf4\xe9\xd0\xd2\xd2\xc2\x8c\x193\xa0\xab\xab\xcb\x96Q\xcaf\ 上得到了这些类型的二进制文件,所以如何从数据库中获取图像,然后将其转换为图像并将该图像插入树视图 table tkinter.
如果存储到数据库中的图像是原始数据,如下例:
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
# sample code to insert record to database
cursor.execute('CREATE TABLE IF NOT EXISTS sample (image BLOB, name TEXT)')
with open('sample.png', 'rb') as f:
data = f.read()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (data, 'Sample'))
cnx.commit()
cursor.close()
cnx.close()
然后你可以使用ImageTk.PhotoImage(data=...)
来创建要在tkinter应用程序中使用的图像。
下面是一个例子:
import sqlite3
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
root = tk.Tk()
s = ttk.Style()
s.configure('Treeview', rowheight=100) # because the image is around 100x100
tree = ttk.Treeview(root, columns=['Name'], height=2)
tree.pack()
tree.heading('#0', text='Image')
tree.heading('Name', text='Name')
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
cursor.execute('SELECT * FROM sample')
imglist = []
for rec in cursor:
img = ImageTk.PhotoImage(data=rec[0])
tree.insert('', 'end', image=img, values=rec[1:])
imglist.append(img)
cursor.close()
cnx.close()
root.mainloop()
结果: