title: retrieve image from sqlite and add to listbox tkinter python
body: <p>Im trying to get a blob image from my db and insert it to listbox but i cant.
  any ways to do it?</p>
  <p>My entire code:</p>
  <pre><code>from tkinter import *
  from tkinter import messagebox, Tk, Label, filedialog, font
  from blobdb import Database
  from PIL import ImageTk, Image
  from resizeimage import resizeimage
  import re
  import sqlite3
  
  def fetch_contact ():
      listBox.delete (0, END)
  
      sqliteConnection = sqlite3.connect('store.db')
      cursor = sqliteConnection.cursor()
      print(&quot;Connected to SQLite&quot;)
      sqlite_select_query = &quot;&quot;&quot;SELECT * from contacts&quot;&quot;&quot;
      cursor.execute(sqlite_select_query)
      records = cursor.fetchall()
      for row in records:
          listBox.insert (END, (row[1], row[2]))
  
  
  def convertToBinaryData(filename):
      # Convert digital data to binary format
      with open(filename, 'rb') as file:
          blobData = file.read()
      return blobData
  
  
  def insertBLOB(id, name, last_name, photo, phone_num, email, home_address):
      print(id, name, last_name, photo, phone_num, email)
      try:
          sqliteConnection = sqlite3.connect('store.db')
          cursor = sqliteConnection.cursor()
          print(&quot;Connected to SQLite&quot;)
          sqlite_insert_blob_query = &quot;&quot;&quot; INSERT INTO contacts (id, name, last_name, photo, phone_num, email, home_address) VALUES (?, ?, ?, ?, ?, ?, ?)&quot;&quot;&quot;
  
          empPhoto = convertToBinaryData(photo)
          # Convert data into tuple format
          data_tuple = (id, name, last_name, empPhoto,
                        phone_num, email, home_address)
          cursor.execute(sqlite_insert_blob_query, data_tuple)
          sqliteConnection.commit()
          listBox.delete (0, END)
          fetch_contact()
  
          print(&quot;Image and file inserted successfully as a BLOB into a table&quot;)
          cursor.close()
  
      except sqlite3.Error as error:
          print(&quot;Failed to insert blob data into sqlite table&quot;, error)
      finally:
          if (sqliteConnection):
              sqliteConnection.close()
              print(&quot;the sqlite connection is closed&quot;)
  
  
  def clear_contacts():
      global photo, image_entry, name_id, image, show_image, image_label
      photo = 'no_image.jpg'
      image_entry = Entry(app, textvariable=photo)
      name_id = ''
      name_entry.delete(0, END)
  
      lastname_entry.delete(0, END)
  
      phone_entry.delete(0, END)
  
      email_entry.delete(0, END)
  
      address_entry.delete(0, END)
  
  
  def send_data():
      global photo, image_entry
      regex_email = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
      regex_names = '^[a-zA-Zا-ی]'
      regex_phone = '^[0-9]'
  
      def check_email(email):
          if (re.search(regex_email, email)):
              return True
          else:
              return False
  
      def check_names(name):
          if (re.search(regex_names, name)):
              return True
          else:
              return False
  
      def check_phone(phone):
          if (re.search(regex_phone, phone)):
              return True
          else:
              return False
      if name_value.get() == '' or lastname_value.get() == '' or phone_value.get() == '' or email_value.get() == '' or address_value.get() == '':
          messagebox.showerror(&quot;Required Fields&quot;, &quot;Please include all fields&quot;)
          return
      if not check_names(name_value.get()):
          messagebox.showerror(
              &quot;Error&quot;, &quot;Please Write Your Firstname Corectly(Just a-z A-Z)&quot;)
          return
      if not check_names(lastname_value.get()):
          messagebox.showerror(
              &quot;Error&quot;, &quot;Please Write Your Lastname Corectly(Just a-z A-Z)&quot;)
          return
      if not check_phone(phone_value.get()):
          messagebox.showerror(
              &quot;Error&quot;, &quot;Please Write Your Phone Number Corectly(Just 0-9)&quot;)
          return
      if not check_email(email_value.get()):
          messagebox.showerror(&quot;Error&quot;, &quot;This Email Is Not A Valid Email&quot;)
          return
      if photo == '':
          photo = 'no_image.jpg'
          image_entry = Entry(app, textvariable=photo)
  
      name_id = id(name_value.get())
  
      insertBLOB(name_id, name_value.get(), lastname_value.get(), photo, phone_value.get(), email_value.get(), address_value.get())
  
  def open_image ():
      global photo, image_entry, image, show_image, image_label
      photo = filedialog.askopenfilename (initialdir=&quot;/&quot;, title='Select A File', filetypes = (('png files', '*.png'), ('jpg files', '*.jpg')))
      image_entry = Entry (app, textvariable=photo)
      image = photo[:]
      show_image = ImageTk.PhotoImage (Image.open (image).resize ((80, 80), Image.ANTIALIAS))
      image_label = Label (app, text='', image=show_image)
      image_label.grid (row = 0, column = 1)
  
  
  app = Tk()
  app.title('Contact List')
  app.geometry('400x600')
  app.configure(background='RoyalBlue4')
  icon_image = ImageTk.PhotoImage(Image.open('Alora_icon.ico').resize((20, 20), Image.ANTIALIAS))
  app.iconbitmap(icon_image)
  
  regex_email = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
  regex_names = '^[a-zA-Zا-ی]'
  regex_phone = '^[0-9]'
  
  # list box
  sf= font.Font (family='Helvetica', size=36, weight='bold')
  listBox = Listbox (app, height = 6, width = 15, font = sf)
  listBox.place (x = 50, y = 280)
  
  # Scrollbar
  scrollbar = Scrollbar (app)
  scrollbar.place (x = 378, y = 280)
  
  # set scrollbar to list
  listBox.configure (yscrollcommand = scrollbar.set)
  scrollbar.configure (command = listBox.yview)
  
  fetch_contact()
  
  # name input
  name_value = StringVar ()
  name = Label (app, text=&quot;FIRST NAME&quot;)
  name.grid (row=1, column=0, sticky=W)
  name_entry = Entry (app, textvariable=name_value)
  name_entry.grid (row=1, column=1, sticky=W, padx = 4, pady = 2)
  
  # last name input
  lastname_value = StringVar ()
  lastname = Label (app, text=&quot;LAST NAME&quot;)
  lastname.grid (row=2, column=0, sticky=W)
  lastname_entry = Entry (app, textvariable=lastname_value)
  lastname_entry.grid (row=2, column=1, sticky=W, padx = 4, pady = 2)
  
  # Image
  photo = 'no_image.jpg'
  image_entry = Entry (app, textvariable=photo)
  image = photo[:]
  show_image = ImageTk.PhotoImage (Image.open (image).resize ((80, 80), Image.ANTIALIAS))
  image_label = Label (app, text='', image=show_image)
  image_label.grid (row = 0, column = 1, padx = 4, pady = 2)
  
  # phone input
  phone_value = StringVar ()
  phone = Label (app, text=&quot;PHONE&quot;)
  phone.grid (row=3, column=0, sticky=W)
  phone_entry = Entry (app, textvariable=phone_value)
  phone_entry.grid (row=3, column=1, sticky=W, padx = 4, pady = 2)
  
  # Email input
  email_value = StringVar ()
  email = Label (app, text=&quot;EMAIL&quot;)
  email.grid (row=4, column=0, sticky=W)
  email_entry = Entry (app, textvariable=email_value)
  email_entry.grid (row=4, column=1, sticky=W, padx = 4, pady = 2)
  
  # home address input
  address_value = StringVar ()
  address = Label (app, text=&quot;HOME ADDRESS&quot;)
  address.grid (row=5, column=0, sticky=W)
  address_entry = Entry (app, textvariable=address_value)
  address_entry.grid (row=5, column=1, sticky=W, padx = 4, pady = 2)
  
  name_id = id (name_value.get())
  
   
  # menubar = Menu (app)
  # filemenu = Menu(menubar, tearoff=0)
  # filemenu.add_command(label=&quot;Add Contact&quot;, command= print())
  # menubar.add_cascade(label=&quot;File&quot;, menu=filemenu)
  # app.config(menu=menubar)
  
  add_btn = Button (app, text='ADD CONTACTS', width=12, command=lambda: send_data ())
  add_btn.grid (row=7, column=1)
  
  add_photo_btn = Button (app, text='ADD PHOTO', width=12, command=lambda: open_image ())
  add_photo_btn.grid (row=0, column=0)
  
  # Start program
  app.mainloop ()
  </code></pre>