如何将图像插入到sqlite3数据库中?

How to insert image into sqlite3 database?

这是我的部分代码:

import sqlite3
import tkinter
import time
import PIL.Image, PIL.ImageTk
from PIL import Image,ImageTk
import cv2
import numpy as np
from tkinter import Tk, Label, Button, Entry, Toplevel

r=Tk()
conn = sqlite3.connect('datastorage.db')
print("Opened database successfully");

def snapshot(self):
             # Get a frame from the video source
            ret, frame = self.vid.get_frame()

我正在从视频中捕获帧,我需要将其插入到包含文本列和 blob(二进制大对象)列的数据库中。还有其他类似的问题建议转换为字符串并存储,但由于我已经存储了 blob 格式的图像,并且我正在使用 decode 提取它们,如下面的代码所示,我只需要存储 blob。

blob_data=row[1]
                nparr  = np.frombuffer(blob_data, np.uint8)
                img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
                image1=cv2.resize(img_np,(260,200))
                #cv2.imshow("data",image1)
                #break
                #Rearrang the color channel
                b,g,r = cv2.split(image1)
                image1 = cv2.merge((r,g,b))
                hsv1=cv2.cvtColor(image1, cv2.COLOR_RGB2HSV)
                kernel2 = np.ones((3,3),np.uint8)

我尝试使用以下查询:

cursor=conn.execute("create table if not exists user_6 (id text, img blob)")
cursor=conn.execute("insert into user_6 values (?,?)",(ins,sqlite3.Binary(frame)))

但我无法使用与显示所有其他条目相同的方法来显示它。用于显示的代码是第二个代码块。我遇到了如图所示的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ABC\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\ABC\Desktop\Python tut\gui databse.py", line 71, in display
    image1=cv2.resize(img_np,(130,100))
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

谁能帮帮我?

我能够使用以下代码完成此操作:

img_str = cv2.imencode('.jpg', frame)[1].tostring()
cursor=conn.execute("create table if not exists user_6 (id text, img blob)")
cursor=conn.execute("insert into user_6 values (?,?)",(ins,img_str))
conn.commit()

虽然图像中的颜色与捕获的图像不同。