"can't escape _io.BufferedRandom to binary" when trying to insert image to BYTEA column

Viewed 2579

So I'm trying to upload an image through bottle, and insert it into postgres BYTEA column with psycopg2, but I'm running into this error:

TypeError: can't escape _io.BufferedRandom to binary

From the cursor.excute() line that would insert the data.

Here's my code:

@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

What am I doing wrong?

1 Answers
Related