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
  Latex image too big on tkinter using sympy 4096 1 142 Nov-05-2020, 08:05 AM
Last Post: DPaul
Photo Tkinter TEXT background image _ShevaKadu 5 860 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  Tkinter - How can I extend a label widget? TurboC 2 213 Oct-13-2020, 12:15 PM
Last Post: zazas321
  Scrollable big image in a window (TKinter) Prospekteur 3 260 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Tkinter: How to assign calculated value to a Label LoneStar 7 389 Sep-03-2020, 08:19 PM
Last Post: LoneStar
  Adding an image to a tkinter window djwilson0495 2 775 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  Create image on a Toplevel using tkinter ViktorWong 3 1,647 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  changing tkinter label from thread nanok66 3 675 Jun-07-2020, 01:37 AM
Last Post: nanok66
  [Tkinter] Tkinter Logo Image Evil_Patrick 1 735 May-25-2020, 05:12 AM
Last Post: DT2000
  [Tkinter] Returning always the last image into Label Lucas_Ribeiro 1 542 May-08-2020, 05:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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