By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

hi, please how do i display image stored in my database in tkinter window

P: 1
Expand|Select|Wrap|Line Numbers
  1. import tkinter as tk
  2. from tkinter import *
  3. from PIL import Image, ImageTk
  4. from tkinter import filedialog
  5. from tkinter import ttk
  6. import sqlite3 as sq
  7. import base64
  8.  
  9. window= tk.Tk()
  10. window.geometry('70x70')
  11.  
  12.  
  13. load = Image.open("hqdefault.jpg")
  14. render = ImageTk.PhotoImage(load)
  15. img = Label(window, image=render,width=10,height=10)
  16. img.image = render
  17. img.place(x=0, y=0)
  18.  
  19. def browse():
  20.  
  21.     filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
  22.     print (filename)
  23.     entry.delete(0,END)
  24.     entry.insert(tk.END, filename)
  25.  
  26. button=ttk.Button(text='Browse',command=browse)
  27. button.grid()
  28.  
  29. data=tk.StringVar()
  30. entry=ttk.Entry(textvariable=data)
  31. entry.grid()
  32.  
  33. name='Davies'
  34.  
  35. db=sq.connect('img.db')
  36. conn=db.cursor()
  37. conn.execute('''
  38. CREATE TABLE IF NOT EXISTS images(name TEXT, data BLOB)''')
  39. db.commit()
  40. db.close()
  41. def write_file(data, filename):
  42.     # Convert binary data to proper format and write it on Hard Disk
  43.     with open(filename, 'wb') as file:
  44.         file.write(data)
  45.  
  46. def display():
  47.     db=sq.connect('img.db')
  48.     conn=db.cursor()
  49.     conn.execute('''SELECT * FROM images''')
  50.     row=conn.fetchall()
  51.  
  52.     for i in row:
  53.  
  54.         it=i[1]
  55.         this= open(str(len(i[1])),'wb')
  56.         this.write(base64string.decode('base64'))
  57.         this.close
  58.         imgd=ImageTk.PhotoImage(this)
  59.         panel=tk.Label(image=imgd,height=28,width=30)
  60.         panel.image = imgd
  61.         panel.grid()
  62.  
  63.  
  64.     db.close()
  65.  
  66.  
  67.  
  68. def add_img():
  69.     db=sq.connect('img.db')
  70.     conn=db.cursor()
  71.  
  72.     with open(str(data.get()),'rb') as f:
  73.         file=f.read()
  74.     with open('yhu.png','wb') as p:
  75.         p.write(f)
  76.     conn.execute(''' INSERT INTO images(name,data)VALUES(?,?)''',(name,file))
  77.  
  78.     db.commit()
  79.     db.close()
  80.  
  81.  
  82. upload=ttk.Button(text='Upload',command=add_img)
  83. upload.grid()
  84.  
  85. display=ttk.Button(text='Display', command=display)
  86. display.grid()
  87.  
  88.  
  89.  
  90. window.mainloop()
4 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 615
What is the difference between your two blocks of code?

Expand|Select|Wrap|Line Numbers
  1. load = Image.open("hqdefault.jpg")
  2. render = ImageTk.PhotoImage(load)
  3. img = Label(window, image=render,width=10,height=10)
and
Expand|Select|Wrap|Line Numbers
  1.         this= open(str(len(i[1])),'wb')
  2.         ## and note that "this" no longer exists because you closed the file
  3.         imgd=ImageTk.PhotoImage(this)
  4.         panel=tk.Label(image=imgd,height=28,width=30) 
4 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.