0
votes

I have a python code and I want to save an image into the database and this image is an object (Image)

Here is my table which I want to save on it

CREATE TABLE project ( email varchar(100) COLLATE utf8_bin DEFAULT NULL, project_name varchar(200) COLLATE utf8_bin NOT NULL, panelimg longblob NOT NULL, status int(1) NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

and here is what I write but it didn't work correctly

conv = convertToBinaryData(origimg)
cursor.execute("UPDATE project SET panelimg=origimg where status = 0 and email = '[email protected]'")

where the origimage in the image as the object (Image)

and here is the function of the convert to binary :

def convertToBinaryData(filename):
    #Convert digital data to binary format
    with open(filename, 'rb') as file:
        binaryData = file.read()
    return binaryData

I got this error, so I need help

File "K-mean4-draw6-database.py", line 17, in convertToBinaryData with open(filename, 'rb') as file: TypeError: expected str, bytes or os.PathLike object, not Image

any suggestions please, Thank you

1

1 Answers

0
votes

You can convert the image into base64 string whenever you need to store it in the db and you can use the reverse step at the time of fetching data from db.

Here the code how you can use base64:

import base64

with open("filepath/filename", "rb") as imageFile:
    image_string = base64.b64encode(imageFile.read())