python - 如何将存储在 sqlite 中的图像显示到 tkinter 窗口中
问题描述
这是将输入框中的图像添加到数据库中的部分
def add_img():
db=sq.connect('img.db')
conn=db.cursor()
with open(str(data.get()),'rb') as f:
file=f.read()
with open('yhu.png','wb') as p:
p.write(f)
conn.execute(''' INSERT INTO images(name,data)VALUES(?,?)''',(name,file))
db.commit()
db.close()
这部分应该将图像显示到 tkiinter 窗口中
def display():
db=sq.connect('img.db')
conn=db.cursor()
conn.execute('''SELECT * FROM images''')
row=conn.fetchall()
for i in row:
it=i[1]
this= open(str(len(i[1])),'wb')
this.write(base64string.decode('base64'))
this.close
imgd=ImageTk.PhotoImage(this)
panel=tk.Label(image=imgd,height=28,width=30)
panel.image = imgd
panel.grid()
db.close()
解决方案
该类PhotoImage
可以接受二进制数据作为参数,而无需将其保存到文件中:
imgd = ImageTk.PhotoImage(data=it)
以上假设it
是来自原始图像文件的二进制数据。
推荐阅读
- wordpress - Woocommerce 按 ID 列出的产品列表仅返回一个产品,而不是多个选择
- github - 链接到您的 GitHub 新闻提要?
- javascript - 失去焦点后保持插入符号显示在div中
- java - 在休眠中使用 bean 验证
- erlang - 如何将输入作为元组并将其存储在 Erlang 的变量中?
- firebase - Kotlin Firebase:无法将 java.lang.String 类型的对象转换为 com.example.Model.perizinan 类型
- wcf - 将连接的 Web 服务 WCF 引用添加到 .Net Core API 时出错
- r - R基于参考数据集创建虚拟数据集
- arrays - 如何在二维数组上使用 v-for
- c# - 是否有理由避免包含 System.Net.Http?