wxPython: images from URLs

Kevin Altis altis at semi-retired.com
Mon Jan 26 17:03:25 EST 2004


"Jonathan Daugherty" <cygnus at cprogrammer.org> wrote in message
news:mailman.810.1075149139.12720.python-list at python.org...
> Does anyone here know if the wxImage class in wxPython supports dislaying
> images from URLs?

Yes, the trick is to use StringIO to convert the data rather than saving to
a file and loading it from disk. Here's a concrete example...

ka
---

import urllib
from wxPython import wx
from cStringIO import StringIO

# I'll assume you already have an app, frame...
# to draw into if that's what you want to do

wx.wxInitAllImageHandlers()

# here's a real URL for testing purposes
url = 'http://pythoncard.sourceforge.net/images/addresses_01.png'

try:
    fp = urllib.urlopen(url)
    data = fp.read()
    fp.close()
    img = wx.wxImageFromStream(StringIO(data))
except:
    # decide what you want to do in case of errors
    # there could be a problem getting the data
    # or the data might not be a valid jpeg, png...
    pass

# now you can do whatever you want with the image





More information about the Python-list mailing list