0
votes

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

1
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
yeah pretty sure. To test I used print (picData) then compared the output with the image in a hex editor and they matched. - user2148781
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
You asked the exact same question yesterday, with the only variation being your wording and label being renamed from label_9 stackoverflow.com/questions/15411330/… - smont
oh well got my answers so dont care - user2148781

1 Answers

1
votes

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.