gpt4 book ai didi

python - "can' t escape _io.BufferedRandom to binary"试图将图像插入 BYTEA 列时

转载 作者:行者123 更新时间:2023-11-29 13:19:47 24 4
gpt4 key购买 nike

所以我尝试通过 bottle 上传图像,并使用 psycopg2 将其插入到 postgres BYTEA 列中,但我遇到了这个错误:

TypeError: can't escape _io.BufferedRandom to binary

从将插入数据的 cursor.excute() 行。

这是我的代码:

@route('/images', method='POST')
def upload_image():
upload = request.files.get('image')
img = Image.open(upload.file) # Pillow
binary = psycopg2.Binary(upload.file)
cursor = connection.cursor()
id = cursor.execute(
'''
INSERT INTO image (filename, data, width, height)
VALUES (%s, %s, %s, %s)
RETURNING id
''',
(upload.filename, binary, img.width, img.height)
)
return id

我做错了什么?

最佳答案

我想知道问题是否出在 psycopg2.Binary 需要一个字符串但正在接收一个类似文件的对象。您是否尝试过类似的方法?

binary = psycopg2.Binary(upload.file.read())

注意:您可能需要先查找文件的开头,因为(我猜)上一行的 Image.open 调用将消耗 中的所有字节上传.file.

关于python - "can' t escape _io.BufferedRandom to binary"试图将图像插入 BYTEA 列时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44046479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com