0

I have a BLOB image in Cloud Server and I want to display it using Tkinter.Label()

Before diving into code I must tell blob file exist in mysql table and record itself does store a row from table correctly. records[0][2] stands for /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwI...

Here is the code snippet:

global img

#Connecting Server
mydb= mysql.connector.connect(**config)
cursor = mydb.cursor(buffered=True)


#Fetching Blob Image to Record
sql_fetch_blob_query = """SELECT * from my-Table where id = %s"""
emp_id=4
cursor.execute(sql_fetch_blob_query, (emp_id,))
record = cursor.fetchall()

#Base64 Encoding
base64_encoded= base64.b64encode(record[0][2])
base64_encoded_string= base64_encoded.decode('utf-8')

#Displaying on Tkinter
root=tk.Tk()
img=tk.PhotoImage(data=base64_encoded_string)
myLabel= tk.Label(root,image=img)
myLabel.pack()
root.mainLoop()

And here is the error i get:

_tkinter.TclError: couldn't recognize image data onimg=tk.PhotoImage(data=base64_encoded_string)line


I tried changing that line to img=tk.PhotoImage(data=record[0][2]) and still the same error.

EDIT:

I print record[0][2] before encoding and the output is: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\

Alli
  • 55
  • 8
  • Based on your update section, the image in the database is the raw data. So you should use `base64_encoded` in `tk.PhotoImage()`: `img = tk.PhotoImage(data=base64_encoded)`. – acw1668 Oct 19 '20 at 05:45
  • 1
    From your update section, the image in the database is in JPEG format which is not supported by `tk.PhotoImage()`. – acw1668 Oct 19 '20 at 05:53

1 Answers1

0

SOLVED!

So it turns out my blob image that I store in cloud server is something PIL.ImageTk.PhotoImage() can use. In other word img=PIL.ImageTk.PhotoImage(data=record[0][2]) is the line I needed! Then I put a label on my tkinter GUI which is myLabel= tk.Label(root,image= img) AND changed root.mainLoop() to root.mainloop().

Now I can upload image files to cloud server and display it on my GUI whenever I want.

Alli
  • 55
  • 8