1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
數(shù)據(jù)類型 BLOB 保存字節(jié),在您的情況下,您嘗試保存拋出錯(cuò)誤的 PIL 對(duì)象。解決方案是轉(zhuǎn)換這些對(duì)象。然后從數(shù)據(jù)庫(kù)獲取的數(shù)據(jù)也會(huì)發(fā)生同樣的情況,但情況相反:
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()
添加回答
舉報(bào)