0

Trying to display a picture from the internet on my GUI window.

So far my code is:

picURL = "https://graph.facebook.com/" + ID + "/picture"
picBytes= urlopen(picURL).read()
picData = io.BytesIO(picBytes)
picPil = Image.open(picData)
picTk = ImageTk.PhotoImage(picPil)
label = Label(image = picTK, bg = "blue").pack()

The problem is all I get is a blue box where the picture should be. How do I fix this?

Using python 3.3 on windows

user2148781
  • 129
  • 1
  • 1
  • 11
  • Works for me using an image stored on disk... are you sure that the `picData` is okay, i.e. that you actually receive the image? – tobias_k Mar 15 '13 at 14:01
  • yeah pretty sure. To test I used `print (picData)` then compared the output with the image in a hex editor and they matched. – user2148781 Mar 15 '13 at 14:04
  • Also works using a random pic from the internet (though not from facebook). Can you show the rest of the code? Somewhere you have to create a Tk instance etc. ... Update: also tested with a random facebook ID. Works. (using Python 2.7 on Linux, though...) – tobias_k Mar 15 '13 at 14:06
  • You asked the exact same question yesterday, with the only variation being your wording and `label` being renamed from `label_9` http://stackoverflow.com/questions/15411330/retrieve-jpeg-image-from-a-redirected-url-and-display-it-on-a-gui-window – smont Mar 15 '13 at 18:00
  • oh well got my answers so dont care – user2148781 Mar 19 '13 at 19:58

1 Answers1

1

This is a wild guess now, but I just remembered a similar problem. I was able to reproduce your "blue box" this way, so this might be your problem, too. I'll just give it a try.

I assume that the PhotoImage is created in some other scope (maybe a method showImage(self, id) or something like that) and no reference to it is kept beyond the scope of this method. This has the effect that the PhotoImage will be garbage-collected at the end of this method, even though it is used in the Label!

Try creating a variable that exists for the whole life time of the frame and bind the PhotoImage to that variable (e.g. self.images[ID] when using a class for the GUI, or some global variable otherwise). If I am right, and this is indeed the problem, then this should help.

Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • im not using classes. What other code did you want to see? As you said it works on 2.7 im guessing the problem is a change of syntax from 2.7 to 3.3 – user2148781 Mar 15 '13 at 15:18
  • @user2148781 I still think its due to the GC. Even if you do not use classes, it might be the case that the `PhotoImage` is created in some other scope and discarded later on. Does the image still not show if you bind the `PhotoImage` to a global variable? What if you put everything on top level, like in your snippet, just with added imports and Tk-initialization? – tobias_k Mar 15 '13 at 15:26
  • So you think I might need to make my picTk image global? What if we go to a chat and ill put all my code in a paste bin – user2148781 Mar 15 '13 at 15:32
  • It worked. Have no idea how/why but it worked. thank you so much. Do you now how I would mae it bigger as its only a thumbnail atm – user2148781 Mar 15 '13 at 15:40
  • Yes, this is a pretty strange behaviour in Python/Tkinter, which stumped me, too, when I first encountered it. I would expect the Label to establish some lasting reference to the PhotoImage, so it survives GC, but apparantly that's not the case. Glad it worked out for you! – tobias_k Mar 15 '13 at 15:42