tkinter: Image to Label - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: tkinter: Image to Label (/thread-30621.html) Pages:
1
2
|
tkinter: Image to Label - Maryan - Oct-28-2020 I have some problem with my function. I want to open image(filedialog) and display the image into label. def openImg(): fileimg = filedialog.askopenfile(mode = 'rb+', initialdir = "/Desktop/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("png", "*.png"))) content = fileimg.read() img = PhotoImage(file = content) my_label = Label(image = img).grid(row = 3, column = 0)Error output: _tkinter.TclError: couldn't open "ÿØÿà►JFIF☺☻☺☺ÿí Photoshop 3.08BIM♦♦g∟☻(bFBMD01000a8d0100001b080000461200004c140000bc150000151f0000292d00008c2e0000de300000ca320000654a0000ÿÛC♠♦♣♠♣♦♠♠♣♠♠ ► ¶♫☼♀►↨¶↑↑↨¶▬▬→↔%▼→∟▬▬↓▼-(0%()(ÿÛC☺ ‼ ‼(→▬→((((((((((((((((((((((((((((((((((((((((((((((((((ÿÂ☺@☺@♥"☺◄☺☻◄☺ÿÄ∟☺♣☺☺☺♥☺☻♦♣ÿÄ→☺♥☺☺☺☺☺☻♥♦♣♠ÿÄ→☺♥☺☺☺☺☺☻♥♦♣♠ÿÚ♀♥☺◄☻◄☺Îr'O©Í♣à¢fAp§b¢5#PB\Wº[sé)ý|ÌçF²+ðõ↔QWR~ôX2ªJq)4¹ºÈH¼¤ÂÌ">¶Ðy®ãô× l25Y=ïX↔â©È 55Sèåçm♥D! ►$m Äæµ =ä.WÓtyÝEªËPda5óÚG²¥ôy³5''³\x♥♂õó◄ÂpD¢DrѸ$ÀyA××Xuùz\▲çÍã£è:}'' ¦õN¬¼H~ÝQyy2z♣↔edÀVcy¶·S§z¼¨ÛË´Rôѵ5©9_w b²;Z>⌂B GH&µ&Á!Ã♦J¬I¤ØHõiS·4°▼K÷r}ÝÁÝÜ↔ÝÀ*[þD y↔Ìêë↑☺M¤Æó¥Äæ õzé2íyéípû▲⌂DPá♥>ÉáZLÈ0Æó'$±∟µoVµZ▬;nnj°>Y♫▲>TKáÆ☺bOLSJ¤WKÖÑôå¶òñì etc.. RE: tkinter: Image to Label - deanhystad - Oct-28-2020 Have you tried to determine where the error is happening? First I would do this: def openImg(): fileimg = filedialog.askopenfile(mode = 'rb+', initialdir = "/Desktop/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("png", "*.png"))) print(type(fileimg), fileimg) return content = fileimg.read() img = PhotoImage(file = content) my_label = Label(image = img).grid(row = 3, column = 0)If fileimg is a file I would move the print and return to see what you are getting back for content. If that looks good I would move things down and look at img. I think you are going to find that you are providing the wrong kind of argument to one of the function calls. Hint Hint RE: tkinter: Image to Label - Maryan - Oct-28-2020 Probably I found the error: img = PhotoImage(file = content) #_tkinter.TclError: couldn't open " ....#ÿÙ": filename is invalid on this platform print(type(img),img) However, I'm not sure how can I fix it :( (Oct-28-2020, 06:16 PM)deanhystad Wrote: Have you tried to determine where the error is happening? First I would do this: RE: tkinter: Image to Label - deanhystad - Oct-28-2020 PhotoImage asks for a file. Why are you reading the file and passing the contents? RE: tkinter: Image to Label - Maryan - Oct-29-2020 If I pass 'fileimg' directly to the PhotoImage(file = fileimg), I'm getting error ( no such file ). So my thought was first to store it in 'memory' and then pass it to photoimage :| With print(type(content),content) it will read the image in bytes <class 'bytes'>. Content suppose to be the Image file that I open it with filedialog. I have only 1-2 months expirence with python, I'm not sure if I can find the solution by myself. Update: img = PhotoImage(content) now is not taking the image from file, but I'm getting another TypeError. #TypeError: __str__ returned non-string (type bytes) #<class 'tkinter.PhotoImage'> (Oct-28-2020, 08:11 PM)deanhystad Wrote: PhotoImage asks for a file. Why are you reading the file and passing the contents? RE: tkinter: Image to Label - deanhystad - Oct-29-2020 Read the fine manual. Guessing what the arguments are is not working RE: tkinter: Image to Label - Maryan - Oct-29-2020 (Oct-29-2020, 12:39 AM)deanhystad Wrote: Read the fine manual. Guessing what the arguments are is not working I couldn't find anything that could help me. I can easily open txt file and display it in label, but for image is just not going to work. :| My last try is below..its terrible. I give up on this. RE: tkinter: Image to Label - deanhystad - Oct-29-2020 Tkinter photoimage doesn't return anything on a google search? We must be on different internets. Try passing the file name. RE: tkinter: Image to Label - buran - Oct-29-2020 https://effbot.org/tkinterbook/photoimage.htm RE: tkinter: Image to Label - Maryan - Oct-29-2020 (Oct-29-2020, 05:57 AM)buran Wrote: https://effbot.org/tkinterbook/photoimage.htm I was testing my code with jpg image, which is not supported by tkinter. I rewrite my code dozen times because of the "_tkinter.TclError: couldn't recognize image data". the code is ok now |