2

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')

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Paul
  • 83
  • 1
  • 8
  • 2
    What do you mean with `filename = addphoto()` ? – Delrius Euphoria Mar 07 '21 at 18:20
  • @CoolCloud I'm trying to call the photo that I've displayed in my frame using that funtion `def addphoto():` though I guess is wrong. I want to convert the photo into BLOB then update to my database, I think that I just need one function to convert and update maybe something like `def convert_update():` ? – Paul Mar 07 '21 at 18:24
  • 1
    Try adding `return path` inside of `addphoto()` and then change to `def update(data):` and then add `update(photo)` inside of `convert_pic`, also change to `c.execute('INSERT INTO registeres VALUES (?)', (data ,))`. – Delrius Euphoria Mar 07 '21 at 18:29
  • @CoolCloud I've tried different things but just working till `def addphoto():` , I only can upload my folder image to my frame. – Paul Mar 07 '21 at 18:45
  • 1
    After adding photo send the file path to bytes function and convert to blob after that send that to database function and insert it. – Delrius Euphoria Mar 07 '21 at 18:52
  • @CoolCloud I was trying but nothing, I'll keep searching thanks mate – Paul Mar 07 '21 at 19:12
  • `filename = addphoto(path)` will give error as it does not accept any arguments. Would be much helpful if you could give the the order in which you use the functions, example: `func1 -> func2 -> func3` – Delrius Euphoria Mar 08 '21 at 16:01

0 Answers0