0

My database contains an image(BLOB) named LOGO. I want to display the image on python TkInter window

 import Tkinter
    window= Tk()
    db= MySQLdb.connect("localhost","root","anup","NursecallDB")
    cursor=db.cursor()
    sql= "SELECT LOGO FROM SYSTEMDETAILS"
    cursor.execute(sql)
    logo=cursor.fetchone()
    img =PhotoImage(logo)
    panel = Tkinter.Label(window, image = img)
    panel.grid(row=0,rowspan=5,columnspan=2)
    window.mainloop()

when I am running this program it shows error at

panel = Tkinter.Label(window, image = img)
TypeError:_str_returned non-string(type tuple) 

3 Answers 3

1

upload the image to a folder and insert the path into database. If logo is the path of the image. then program will work.

Sign up to request clarification or add additional context in comments.

Comments

0

PhotoImage take a string into argument which is the filename of the image you want to load or y Python image object. It cannot take blob as an argument. What you need is to load the image from a buffer (see the method here http://effbot.org/imagingbook/image.htm) and then pass the image to the PhotoImage constructor

1 Comment

<?php if( $_FILES['image']['name'] != "" ) { copy( $_FILES['image']['name'], "/home/pi/image" ) or die( "Could not copy file!"); } else { die("No file specified!"); } ?> I have a RPI server and i am writing the above code to save the uploaded image.it shows could not copy file.
0

If you, like me, don't want to pass the image through disk this is what the code looks like for python3 (following DARK_DUCK suggestion):

    from io import BytesIO
    from PIL import Image, ImageTk

    ...

    logo=cursor.fetchone()

    img = Image.open(BytesIO(logo))    
    phimg = ImageTk.PhotoImage(img)

    panel = Tkinter.Label(window, image = phimg)
    panel.grid(row=0,rowspan=5,columnspan=2)

I think that for python 2.7 you'll have to use StringIO instead of BytesIO. See: Python PIL reading PNG from STDIN

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.