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()
添加回答
举报