Python Forum
tkinter: Image to Label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter: Image to Label
#1
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..
Reply
#2
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
Reply
#3
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:
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
Reply
#4
PhotoImage asks for a file. Why are you reading the file and passing the contents?
Reply
#5
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. Sad

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?
Reply
#6
Read the fine manual. Guessing what the arguments are is not working
Reply
#7
(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.


[Image: qqq.png]
Reply
#8
Tkinter photoimage doesn't return anything on a google search? We must be on different internets. Try passing the file name.
Reply
#9
https://effbot.org/tkinterbook/photoimage.htm
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 404 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,480 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,975 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Load image and show in label ko_fal 8 2,918 Oct-25-2022, 09:20 AM
Last Post: ko_fal
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,053 Aug-15-2022, 07:10 PM
Last Post: menator01
  simple tkinter question function call not opening image gr3yali3n 5 3,303 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,559 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,668 Jun-26-2022, 06:26 PM
Last Post: menator01
  How to redraw an existing image to a different image TkInter zazas321 6 5,749 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  [Tkinter] image in label not showing? rwahdan 2 7,956 Jun-25-2021, 10:27 AM
Last Post: rwahdan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020