为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2023-06-27 13:52:59
from PyQt5.QtWidgets import *from PyQt5 import QtWidgets, QtGuifrom PIL import Image, ImageQt    import cv2 as cvimg =  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 vehicletbdata = 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 标签上显示图像,标签只是出来是空的。
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

数据类型 BLOB 保存字节,在您的情况下,您尝试保存抛出错误的 PIL 对象。解决方案是转换这些对象。然后从数据库获取的数据也会发生同样的情况,但情况相反:


import sqlite3

import io

from PyQt5 import QtGui, QtWidgets


from PIL import Image

import cv2 as cv


img =  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 bytes

f = 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 vehicletb



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


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信