wxPython: images from URLs

Anand Pillai pythonguy at Hotpop.com
Wed Jan 28 15:13:13 CET 2004


I have written some code for this in my PyWiew application
which allows one to open image urls directly.

Copying some relevant code from the application...

self._imgstream = urllib2.urlopen(url).read()
stream=cStringIO.StringIO(self._imgstream)

try:
   img=wxImageFromStream(stream)
except:
   pass

In short you do the following.

1. Use urllib or urllib2 to open the image data stream
2. Make a cStringIO string buffer from the data stream
3. Pass it to "wxImageFromStream()" method to get the
   wxImage object.
4. Display the image directly or by converting to 
   a suitable format using PIL.

In my experience I found that wxWindows tend to
display an error message window when the image is displayed
directly as a wxImage though the image data is quite ok.
(Something like a corrupt stream dialog). So what I have
done in the application is, use PIL to convert the wxImage
to Windows BMP format and then display it. This seems
to work for all images.

HTH.

-Anand

          

Tim Roberts <timr at probo.com> wrote in message news:<fgse10l8pae12p0els55fsujllq2irrj0l at 4ax.com>...
> Jonathan Daugherty <cygnus at cprogrammer.org> wrote:
> >
> >Does anyone here know if the wxImage class in wxPython supports dislaying
> >images from URLs?
> 
> wxImage will read from a file or from a wxWindows stream.  It won't
> download from a web site, but that's trivially easy using something like
> urllib.



More information about the Python-list mailing list