python-guide icon indicating copy to clipboard operation
python-guide copied to clipboard

How can i display image from mysql to python gui ?

Open 2020Yasir-Sahibzada opened this issue 4 years ago • 2 comments

I am trying , like this :

from tkinter import * from io import BytesIO window= Tk() db=MySQLdb.connect("localhost","root","??","??") cursor=db.cursor() sql= "SELECT LOGO FROM SYSTEMDETAILS" cursor.execute(sql) logo=cursor.fetchone() img = Image.open(BytesIO(logo)) phimg = ImageTk.PhotoImage(img) panel = Label(window, image = phimg) panel.pack()

When i run this, it show error in line 9 , it want logo as an object but in here its tuple

2020Yasir-Sahibzada avatar Jan 08 '21 21:01 2020Yasir-Sahibzada

Hello @2020Yasir-Sahibzada,

I think you can try to access like this,

logo = cursor.fetchone()
img_data = logo[0]

And after that you can use the BytesIO class to wrap the image data,

img = Image.open(BytesIO(img_data))

somanyadav avatar Dec 23 '22 12:12 somanyadav

Hey @2020Yasir-Sahibzada I think you didn't included mysql imports for the code adding them might solve your issue

anuragdaksh7 avatar Jun 04 '23 07:06 anuragdaksh7