Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python字节对象写入pgSQL bytea列:字节字符串中的语法错误_Python_Postgresql_Image_Byte_Bytea - Fatal编程技术网

将python字节对象写入pgSQL bytea列:字节字符串中的语法错误

将python字节对象写入pgSQL bytea列:字节字符串中的语法错误,python,postgresql,image,byte,bytea,Python,Postgresql,Image,Byte,Bytea,我正在编写一个工具,对测试车辆的CAN数据进行解码和分析,并将其与摄像头画面一起显示 对于相机镜头,我使用openCV从提供的.avi文件中获取帧,然后将其传递给PIL以降低质量,并裁剪出图像的一个角,其中使用pytesseract OCR识别毫秒历元时间戳 接下来,我想将该图像作为bytea存储在我的postgreSQL数据库中 我的代码是这样工作的: 这是上述代码的简化版本: from io import BytesIO from PIL import Image # Image actu

我正在编写一个工具,对测试车辆的CAN数据进行解码和分析,并将其与摄像头画面一起显示

对于相机镜头,我使用openCV从提供的.avi文件中获取帧,然后将其传递给PIL以降低质量,并裁剪出图像的一个角,其中使用pytesseract OCR识别毫秒历元时间戳

接下来,我想将该图像作为bytea存储在我的postgreSQL数据库中

我的代码是这样工作的: 这是上述代码的简化版本:

from io import BytesIO
from PIL import Image

# Image actually comes from cv2, in this case i just use a local jpg as example
image_path = 'meme.jpg'
img = Image.open(image_path)

# Dictionary for the processed Data
img_dict = {'TestDriveID': [], 'TimeStamp':[], 'Data':[]}

# here i store the image to a BytesIO(), reducing quality to 20%
buffer = BytesIO()
img.save(buffer, 'JPEG', quality=20)

# the timestamp is actually obtained via ocr, here i use an arbitrary one.
timestamp = 1619435452340

# write the information to a dictionary to later pass it on to the uploading
# code and/or further uses
img_dict['TestDriveID'].append('test123')
img_dict['TimeStamp'].append(timestamp)
img_dict['Data'].append(buffer)
然后将此字典传递给应该上载图像的函数:

import psycopg2
from sqlalchemy import create_engine
import io

# set up connection definition
sql_con_str = f'{driver}://{user}:{password}@{host}:{port}/{dbname}'
# create engine
sql_con = create_engine(sql_con_str)
# create a raw_connection object from the pgsql engine
raw_con = sql_con.raw_connection()
# define a cursor for the insertion
cur = raw_con.cursor()

for i in range(0, len(img_dict['TestDriveID'])):
    buffer = (img_dict['Data'][i])
    buffer.seek(0)

    cur.execute("""INSERT INTO images ("TestDriveID", "TimeStamp", "Data") 
                    VALUES ('%s', %s, %s)""" %(img_dict['TestDriveID'][i],img_dict['TimeStamp'][i],buffer.read()))
    raw_con.commit()

raw_con.close()
当我执行此代码时,会出现以下异常:

---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
<ipython-input-77-25a01ac78ad2> in <module>
     52     buffer.seek(0)
     53 
---> 54     cur.execute("""INSERT INTO svd_images ("TestDriveID", "TimeStamp", "Data") 
     55                     VALUES ('%s', %s, %s)""" %(img_dict['TestDriveID'][i],img_dict['TimeStamp'][i],buffer.read()))
     56     raw_con.commit()

SyntaxError: syntax error at or near "("
LINE 2: ...1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:...
                                                             ^
---------------------------------------------------------------------------
SyntaxError回溯(最近一次调用上次)
在里面
52缓冲区寻道(0)
53
--->54 cur.execute(““”插入到svd_图像中(“TestDriveID”、“时间戳”、“数据”)
55个值('%s',%s,%s)“”%(img_dict['TestDriveID'][i],img_dict['TimeStamp'][i],buffer.read())
56原始合同提交()
SyntaxError:在“(”处或附近出现语法错误
第2行:…1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:。。。
^
我如何解决这个问题? 还是有更有效的方法? 我计划这样一次插入45000张图像。(30分钟视频,25fps,27kb/帧)

提前感谢,我希望我提供了足够的信息,我对这一点很陌生