绑定参数 4 时出错 - 可能不受支持的类型。图像和 blob 图像不会显示在 QTLabel 上

from PyQt5.QtWidgets import *

from PyQt5 import QtWidgets, QtGui


from PIL import Image, ImageQt    

import cv2 as cv

img =  cv.imread('1.1 cat.jpeg.jpeg')

im = Image.fromarray(img)

im.save('file.png')


con = lite.connect('Final_Avirs.db')

cur = con.cursor()

def createtb():

    queryveh = """CREATE TABLE IF NOT EXISTS VehicleTB(Scan_DI  INTEGER PRIMARY KEY NOT NULL UNIQUE,

    Vehicle_number  TEXT NOT NULL, Vehicle_type TEXT NOT NULL, Cam_loc  TEXT NOT NULL, Date_Time    TEXT NOT NULL, 

    Vehicle_number_pic BLOB NOT NULL) """

 


    cur.execute(queryveh)

    con.commit()


def vehicledetailsquery(vn, vt, cl, dt, vnp):

    scan_id = vn + "1"

    query = " INSERT INTO 'VehicleTB' ( Vehicle_number, Vehicle_type, Cam_loc, " \

            "Date_Time, Vehicle_number_pic ) VALUES( ?, ?, ?, ?, ?)"

    cur.execute(query, (vn, vt, cl, dt, vnp))

    con.commit()

    

img = Image.open('file.png')

createtb()


vehicledetailsquery('aswe23','2039230', 'cam-2', '23343', img )


app = QtWidgets.QApplication([])

def vehicletbquery():

    query = "SELECT * from VehicleTB"

    vehicletb = cur.execute(query).fetchall()

    return vehicletb

data = vehicletbquery()


ww = QtWidgets.QLabel()

for i, d in enumerate(data):

    if i == 6:

        ww.w.setPixmap(QtGui.QPixmap.fromImage(d))

        

ww.show()

app.exec()

我知道问题出在我的图像值上,但我不知道如何处理它,我能做的最好的是将枕头图像转换为字符串,然后这样做,我可以在 PYQT 标签上显示图像,标签只是出来是空的。


慕村225694
浏览 142回答 1
1回答

喵喵时光机

数据类型 BLOB 保存字节,在您的情况下,您尝试保存抛出错误的 PIL 对象。解决方案是转换这些对象。然后从数据库获取的数据也会发生同样的情况,但情况相反:import sqlite3import iofrom PyQt5 import QtGui, QtWidgetsfrom PIL import Imageimport cv2 as cvimg =  cv.imread('1.1 cat.jpeg.jpeg')im = Image.fromarray(img)im.save("file.png")con = sqlite3.connect("Final_Avirs.db")cur = con.cursor()def createtb():    queryveh = """CREATE TABLE IF NOT EXISTS VehicleTB(Scan_DI  INTEGER PRIMARY KEY NOT NULL UNIQUE,    Vehicle_number  TEXT NOT NULL, Vehicle_type TEXT NOT NULL, Cam_loc  TEXT NOT NULL, Date_Time    TEXT NOT NULL,     Vehicle_number_pic BLOB NOT NULL) """    cur.execute(queryveh)    con.commit()def vehicledetailsquery(vn, vt, cl, dt, vnp):    scan_id = vn + "1"    query = (        " INSERT INTO 'VehicleTB' ( Vehicle_number, Vehicle_type, Cam_loc, "        "Date_Time, Vehicle_number_pic ) VALUES( ?, ?, ?, ?, ?)"    )    cur.execute(query, (vn, vt, cl, dt, vnp))    con.commit()img = Image.open("file.png")createtb()# convert to bytesf = io.BytesIO()img.save(f, format="PNG")img_bytes = f.getvalue()vehicledetailsquery("aswe23", "2039230", "cam-2", "23343", img_bytes)def vehicletbquery():    query = "SELECT * from VehicleTB"    vehicletb = cur.execute(query).fetchall()    return vehicletbdata = vehicletbquery()app = QtWidgets.QApplication([])scroll_area = QtWidgets.QScrollArea(widgetResizable=True)container = QtWidgets.QWidget()scroll_area.setWidget(container)lay = QtWidgets.QVBoxLayout(container)for row_data in data:    for i, d in enumerate(row_data):        if i == 5:            # QPixmap from bytes            pixmap = QtGui.QPixmap()            pixmap.loadFromData(d)            label = QtWidgets.QLabel()            label.setPixmap(pixmap)            lay.addWidget(label)scroll_area.show()app.exec()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python