如何用Python显示一个来自sqlite3的BLOB对象(图像)

问题描述 投票:0回答:1

我把一张图片作为BLOB保存在sqlite3数据库的列配置文件中--我调用了函数insertBLOB和相关信息。

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

我试图像这样访问图像文件(这样我就可以在Label中显示它)--通过调用函数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

你知道问题出在哪里吗,我该如何解决?我如何从SQLite数据库中访问BLOB对象并将其呈现出来?

python image sqlite blob binary-data
1个回答
0
投票

回溯告诉我们,有什么地方出了问题。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)

然后一切都会好起来的

© www.soinside.com 2019 - 2024. All rights reserved.