如何使用Python显示sqlite3中的BLOB对象(图像)
我将图像保存为sqlite3数据库列配置文件中的BLOB-我使用相关信息调用函数insertBLOB:如何使用Python显示sqlite3中的BLOB对象(图像),python,image,sqlite,blob,binary-data,Python,Image,Sqlite,Blob,Binary Data,我将图像保存为sqlite3数据库列配置文件中的BLOB-我使用相关信息调用函数insertBLOB: sqliteConnection = sqlite3.connect('image_try.db') cursor = sqliteConnection.cursor() cursor.execute("""CREATE TABLE IF NOT EXISTS images ( id INTEGER PRIMARY KEY, fullname TEXT,
sqliteConnection = sqlite3.connect('image_try.db')
cursor = sqliteConnection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY,
fullname TEXT,
username TEXT,
profile BLOB)""")
def convertToBinaryData(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData
def insertBLOB(name, username, photo):
sqliteConnection = sqlite3.connect('image_try.db')
sqliteConnection.text_factory = str
cursor = sqliteConnection.cursor()
sqlite_insert_blob_query = """ INSERT INTO images
(fullname, username, profile) VALUES (?, ?, ?)"""
empPhoto = convertToBinaryData(photo)
data_tuple = (name, username, empPhoto)
cursor.execute(sqlite_insert_blob_query, data_tuple)
sqliteConnection.commit()
我试图通过调用函数readBlobData访问图像文件(以便在标签中显示它):
def writeTofile(data):
# Convert binary data to proper format and write it on Hard Disk
this = open(data, 'rb')
this.open(io.BytesIO(base64.b64decode(data)))
return this
def readBlobData(empId):
try:
sqliteConnection = sqlite3.connect('image_try.db')
sqliteConnection.text_factory = str
cursor = sqliteConnection.cursor()
sql_fetch_blob_query = """SELECT * from images where id = ?"""
cursor.execute(sql_fetch_blob_query, (empId,))
record = cursor.fetchall()
profile = record[0][3] #Blob object
profile = writeTofile(profile)
image = ImageTk.PhotoImage(profile)
image_label = Label(root, image=image)
image_label.photo = image
image_label.pack()
cursor.close()
调用函数readBlobData时,出现以下错误:
Traceback (most recent call last):
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 90, in
<module>
readBlobData(1)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 67, in
readBlobData
profile = writeTofile(profile)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 51, in
writeTofile
this = open(data, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
回溯(最近一次呼叫最后一次):
文件“C:/Users/hilab/PycharmProjects/dafyProject/addimage.py”,第90行,在
readBlobData(1)
文件“C:/Users/hilab/PycharmProjects/dafyProject/addimage.py”,第67行,在
readBlobData
profile=写文件(profile)
文件“C:/Users/hilab/PycharmProjects/dafyProject/addimage.py”,第51行,在
写文件
此=打开(数据“rb”)
TypeError:file()参数1必须是不带空字节的编码字符串,而不是str
你知道是什么问题吗?我怎样才能修好它?如何从SQLite数据库访问BLOB对象并将其显示?回溯告诉我们,
writeToFile
函数出错,特别是当我们试图打开文件时:
profile = writeTofile(profile)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 51, in
writeTofile
this = open(data, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
我们传递给函数的值是从数据库读取的二进制图像数据
profile = record[0][3]
在这个函数中,我们试图使用这个二进制数据作为我们将要读取的文件的名称,以获得某种格式的二进制数据
def writeTofile(data):
# Convert binary data to proper format and write it on Hard Disk
this = open(data, 'rb')
this.open(io.BytesIO(base64.b64decode(data)))
return this
tkinter.PhotoImage
需要文件的路径,根据其文档,因此我们必须从图像字节创建文件:
def writeTofile(data):
# Write it to the Hard Disk
# (ideally with a suitable name and extension)
filename = 'myfile.img'
with open('myfile.img', 'wb') as f:
f.write(data)
return filename
在readBlobData
中:
image = ImageTk.PhotoImage(file=profile)
然后一切都会好起来。回溯告诉我们,
writeToFile
函数出现了问题,特别是当我们试图打开一个文件时:
profile = writeTofile(profile)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 51, in
writeTofile
this = open(data, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
我们传递给函数的值是从数据库读取的二进制图像数据
profile = record[0][3]
在这个函数中,我们试图使用这个二进制数据作为我们将要读取的文件的名称,以获得某种格式的二进制数据
def writeTofile(data):
# Convert binary data to proper format and write it on Hard Disk
this = open(data, 'rb')
this.open(io.BytesIO(base64.b64decode(data)))
return this
tkinter.PhotoImage
需要文件的路径,根据其文档,因此我们必须从图像字节创建文件:
def writeTofile(data):
# Write it to the Hard Disk
# (ideally with a suitable name and extension)
filename = 'myfile.img'
with open('myfile.img', 'wb') as f:
f.write(data)
return filename
在readBlobData
中:
image = ImageTk.PhotoImage(file=profile)
然后一切都会好起来。我是否必须从计算机上的数据库中保存水滴图像才能显示它?(我使用的是一个有多个客户端的服务器)你能不能把这个问题包括完整的错误回溯和用于创建
图像表的创建表语句。我更新了数据库表中的问题@snakecharmerbprofile
应该是磁盘上图像文件的路径,对吗?不,这是图像的“二进制数据”表示。使用字节显示图像。我是否必须从计算机上的数据库中保存Blob图像才能显示它?(我使用的是一个有多个客户端的服务器)你能不能把这个问题包括完整的错误回溯和用于创建图像表的创建表语句。我更新了数据库表中的问题@snakecharmerbprofile
应该是磁盘上图像文件的路径,对吗?不,这是图像的“二进制数据”表示。使用字节显示图像。