0

I have saved the image in an SQLite database using the DB Browser for SQLite as a blob. I want to open the blob data from the database and draw the image in the window.

For that, I am doing something like this:

import sqlite3
from PIL import Image, ImageTk

sql_fetch_blob_query = "SELECT * from credentials where id = ?"
c.execute(sql_fetch_blob_query, (id,))
record = c.fetchall()
for row in record:
      print("Id = ", row[0], "Name = ", row[1])
      photo = row[4]    # image saved in the 5th column of database

      # drawing image to top window
      render = ImageTk.PhotoImage(photo)
      img = Label(image=render)
      img.image = render
      img.place(x=0, y=0)

But it's not working actually and shows nothing in the window. Please help me out in drawing the image to the window from the SQLite database.

Error is the terminal:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "master.py", line 67, in login
    self.drawWin()
  File "master.py", line 102, in drawWin
    self.drawImage(top)
  File "master.py", line 161, in drawImage
    render = ImageTk.PhotoImage(self.photo)
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 114, in __init__
    mode = Image.getmodebase(mode)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 326, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "/usr/lib/python3/dist-packages/PIL/ImageMode.py", line 64, in getmode
    return _modes[mode]
KeyError: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\

This KeyError continues for hundreds of lines.

Thanks!

4
  • Are you aware of this: How to store image in SQLite database and Convert Bytearray to Image Commented Apr 11, 2020 at 13:24
  • How do you store the image to database? Commented Apr 11, 2020 at 14:40
  • @acw1668 I have stored the image manually through the DB Browser for SQLite. Commented Apr 11, 2020 at 15:11
  • I have edited the question and added the error message I get in my terminal Commented Apr 12, 2020 at 12:08

3 Answers 3

2

You need to use io.BytesIO() and Image.open() on the image data:

import sqlite3
from PIL import Image, ImageTk
import io

sql_fetch_blob_query = "SELECT * from credentials where id = ?"
c.execute(sql_fetch_blob_query, (id,))
record = c.fetchall()
for row in record:
      print("Id = ", row[0], "Name = ", row[1])
      photo = row[4]    # image saved in the 5th column of database

      # convert the image data to file object
      fp = io.BytesIO(photo)
      # load the image
      image = Image.open(fp)

      # drawing image to top window
      render = ImageTk.PhotoImage(image)
      img = Label(image=render)
      img.image = render
      img.place(x=0, y=0)
Sign up to request clarification or add additional context in comments.

Comments

1

When you use ImageTk.PhotoImage it expects a PIL Image or a mode string as the first argument.

If you want to feed it raw data, try using the data keyword argument:

render = ImageTk.PhotoImage(data=photo)

Comments

0

#try to use *

render = ImageTk.PhotoImage(*photo)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.