Again, Downloading and Displaying an Image from the Internet in Tkinter

D

Dustan

Nobody answered last time. I guess they wanted me to give it a shot.
Well, here is how I download the image (it's a class method):

def download_image(self):
web_download=self.opener.open(self.url)
save=open("image.jpg","w")
save.writelines(web_download.readlines())
save.close()
web_download.close()

self.opener is urllib.URLopener(), self.url is the url for the image.

I display the image as follows:

self.image=t.Label(self.frame,image=path+"\\image.jpg")

t is Tkinter, path is sys.path[0].
(if sys.path[0] is not the proper way of getting the program's path,
inform me; I hunted it down without any reference to look to)


But the image won't display, using any application (including Tkinter,
paint, Firefox, etc.). I'm assuming the reason it can't be read is
because the image is protected from downloading.

So, once again, is there a better way to download and display an image
using Tkinter?

Did I try hard enough for you? Are you going to help me this time?
 
D

Dustan

I said:
Nobody answered last time. I guess they wanted me to give it a shot.
Well, here is how I download the image (it's a class method):

def download_image(self):
web_download=self.opener.open(self.url)
save=open("image.jpg","w")
save.writelines(web_download.readlines())
save.close()
web_download.close()

self.opener is urllib.URLopener(), self.url is the url for the image.

I display the image as follows:

self.image=t.Label(self.frame,image=path+"\\image.jpg")

t is Tkinter, path is sys.path[0].
(if sys.path[0] is not the proper way of getting the program's path,
inform me; I hunted it down without any reference to look to)


But the image won't display, using any application (including Tkinter,
paint, Firefox, etc.). I'm assuming the reason it can't be read is
because the image is protected from downloading.

So, once again, is there a better way to download and display an image
using Tkinter?

Did I try hard enough for you? Are you going to help me this time?

I should probably also mention, the only reason I downloaded the image
to a file was because I don't know of any other way to do it. I feel no
need to save the image to my hard drive.
 
J

Justin Ezequiel

cannot help you with Tkinter but...

save=open("image.jpg","wb")
save.write(web_download.read())
save.close()

perhaps this would let you open the file in Paint
 
M

Maric Michaud

Le Mardi 06 Juin 2006 03:08, Dustan a écrit :
I should probably also mention, the only reason I downloaded the image
to a file was because I don't know of any other way to do it. I feel no
need to save the image to my hard drive.

using PIL, there is something like this (untested) :

(assuming you have put your image data in a file-like object, ie. a StringIO,
named self._dled_img)

Label(self.frame, image=TkImage(Image.open(self._dled_img)))

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
D

Dustan

Justin said:
cannot help you with Tkinter but...

save=open("image.jpg","wb")
save.write(web_download.read())
save.close()

perhaps this would let you open the file in Paint

Ok, that worked (was it plain w or the writelines/readlines that messed
it up?). But Tkinter still can't find the image. I'm getting an error
message:

TclError: image "C:\Documents and [pathname snipped]" doesn't exist

If it makes a difference, I'm on a Windows XP machine, and don't have
to go cross-platform.
 
F

Fredrik Lundh

Dustan said:
Ok, that worked (was it plain w or the writelines/readlines that messed
it up?).

the plain "w"; very few image files are text files.
> But Tkinter still can't find the image. I'm getting an error
message:

TclError: image "C:\Documents and [pathname snipped]" doesn't exist

If it makes a difference, I'm on a Windows XP machine, and don't have
to go cross-platform.

the "image" option takes a PhotoImage object, not a file name:

http://effbot.org/tkinterbook/photoimage.htm

note that the built-in PhotoImage type only supports a few image
formats; to get support for e.g. PNG and JPEG, you can use PIL which
ships with it's own PhotoImage replacement:

http://effbot.org/imagingbook/imagetk.htm

</F>
 
D

Dustan

Fredrik said:
Dustan said:
Ok, that worked (was it plain w or the writelines/readlines that messed
it up?).

the plain "w"; very few image files are text files.
But Tkinter still can't find the image. I'm getting an error
message:

TclError: image "C:\Documents and [pathname snipped]" doesn't exist

If it makes a difference, I'm on a Windows XP machine, and don't have
to go cross-platform.

the "image" option takes a PhotoImage object, not a file name:

http://effbot.org/tkinterbook/photoimage.htm

note that the built-in PhotoImage type only supports a few image
formats; to get support for e.g. PNG and JPEG, you can use PIL which
ships with it's own PhotoImage replacement:

http://effbot.org/imagingbook/imagetk.htm

Thanks for the information.

The reason I hadn't made any attempt before is well illustrated here; I
didn't have enough information at my disposal to know where to start
and how I it works. My references are more basic-level than that (I do
have a better reference to look to, but it's currently inaccessible).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top