【问题标题】:I want to retrieve a blob from database sqlite3 and convert it into image我想从数据库 sqlite3 中检索一个 blob 并将其转换为图像
【发布时间】:2022-01-11 21:02:24
【问题描述】:

enter image description here# 我得到了这个 Treeview 图像表而不是图像

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 将其插入到树视图表中。将这些值插入表中时,我在图像列 \xfa\xf4\xe9\xd0\xd2\xd2\xc2\x8c\x193\xa0\xab\xab\xcb\x96Q\xcaf\ 上得到了这些类型的二进制文件那么如何从数据库中获取图像,然后将其转换为图像并将此图像插入到树视图表 tkinter 中。

【问题讨论】:

  • 你知道它应该是什么类型的图像吗?
  • 你有将它插入sqlite的代码吗?您知道图像的尺寸(以像素宽乘以像素高)和二进制 blob 中的字节数吗?
  • @kpie 感谢您对代码第一部分的回复:self.con = sqlite3.connect('car Dealership.db') self.cursorObj = self.con.cursor() self .my_row=self.cursorObj.execute('SELECT image, price, carlincesplatenum, caryear, carmodel, carmake FROM Vechicle_info') self.cars_expense_output = self.cursorObj.fetchall() self.imglist=[] for record in self.cars_expense_output: stream=io.BytesIO(record[0]) img=Image.open(stream) img=ImageTk.PhotoImage(img)
  • @kpie 这是第二个:#img.thumbnail((50,50)) self.carstoselecet_expensetree.insert(parent="",index=END,image=img,values=(record [1],record[2],record[3],record[4],record[5],record[6])) self.imglist.append(img)
  • @MarkSetchell 感谢您对代码第一部分的回复:self.con = sqlite3.connect('car Dealership.db') self.cursorObj = self.con.cursor() self .my_row=self.cursorObj.execute('SELECT image, price, carlincesplatenum, caryear, carmodel, carmake FROM Vechicle_info') self.cars_expense_output = self.cursorObj.fetchall() self.imglist=[] for record in self.cars_expense_output: stream=io.BytesIO(record[0]) img=Image.open(stream) img=ImageTk.PhotoImage(img)

标签: python image tkinter blob


【解决方案1】:

如果存储到数据库中的图像是原始数据,如下例:

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

结果:

【讨论】:

  • 题外话:只能#0列包含图片吗?
  • @CoolCloud 我知道是的。
  • @acw1668 感谢您的回复,非常感谢。我尝试应用您的脚本代码,但图像列消失了,Treeview 表只有 5 列而不是 6 列,有没有机会发布我的代码并告诉我它有什么问题,我陷入了这个问题2周
  • @asaadkittaneh 您可以使用您尝试过的代码更新问题。
  • @acw1668 非常感谢它的工作:) 但图像大小和每列记录的位置存在问题。
猜你喜欢
  • 2018-05-30
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 2015-06-09
相关资源
最近更新 更多