How to convert my image displayed in my frame to BLOB in tkinter?

Viewed 119

Hello guys I'm trying to convert a image that I've displayed in my frame in order to INSERT that converted photo to my database.

This is my code to open a picture and display it in my frame:

frame_photo = Frame(top, width=190, height=195)
frame_photo.config(bg="black", highlightcolor='goldenrod', highlightbackground='goldenrod',
                               highlightthickness=4)
frame_photo.place(x=620, y=90)

btn_add = Button(top, text='upload photo', command=lambda : addphoto())
btn_add.place(x=700, y=500)

def addphoto():
    path = filedialog.askopenfilename(filetypes=[("Image File", '.jpg')])
    im = Image.open(path)
    im = im.resize((170, 170), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(im)
    Label(frame_photo, image=img).pack()
    frame_photo.image = img
    return path

I have another button to update the converted picture (BLOB) to my databse, so I was creating two new functions for that update button

def convert_pic(path):
    filename = addphoto(path)
    with open(filename, 'rb') as file:
        photo = file.read()
    return photo

def update(photo):
    data = convert_pic(photo)
    conn = sqlite3.connect('test.db')
    c = conn.cursor()
    c.execute('INSERT INTO testtable VALUES (?)', (data,))
    conn.commit()

btn_update = Button(top, text='update', command=lambda :[update()])
btn_update.place(x='100', y='190')

0 Answers
Related