473,474 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

1 New Member
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()
Jun 20 '19 #1
1 4117
dwblas
626 Recognized Expert Contributor
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) 
Jun 20 '19 #2

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

Similar topics

2
by: Wayne Wengert | last post by:
I am trying to build an ASP.NET application which includes the capability to allow users to save pictures to a SQL Server 2000 database table and then later to display selected photos (usually JPG...
2
by: RedSouljaz | last post by:
Hi, How to display image that was saved in database ms sql server 2000 into picture box. The field type that I use in database is Images I can save to database but cannot show from database. ...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
3
by: RRsonawane11 | last post by:
Hi, I have store image as OLE object in access database and i wan to display that image using ASP.but problem is that binary format is displaying on page...so can i get that image in asp
6
by: vjayis | last post by:
hi i have stored user profile images in an BLOB field., and need to display it as an thumbnail image in their profile page., and i searched through the net and got the result using header...
5
by: SafaaDalloul | last post by:
Please I want know How I can Display Image from database randomly when the web page refresh in Asp.net by C# code
9
by: chabbs | last post by:
Hi, I have my form with some combo box and radio button. And i need to display an image based on the selections. I need to display dynamically the image when the selections are made without...
1
by: gowthambj | last post by:
i have a table named tablenamevehicle which contains about 15 fields. all are text fields and one is image field stored in blob data type. I am able to display all the text from the other fields of...
0
by: anon123456 | last post by:
Hello, I have books_table in the database. which contain Id, title ,subtitle image & others. I have inserted the image in the string form in the database. Now i want to display all records in the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.