wxPython: images from URLs

Kevin Altis altis at semi-retired.com
Thu Jan 29 16:11:42 EST 2004


"Anand Pillai" <pythonguy at Hotpop.com> wrote in message
news:84fc4588.0401280613.771ac07 at posting.google.com...
> 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.

In general you don't display a wxImage object directly. In wxWindows,
wxImage is the platform-independent image class and wxBitmap is the
platform-specific image class. So, when you set an image to be used with a
wxStaticBitmap or wxBitmapButton or draw to a wxDC you use a wxBitmap.
Certain image operations not yet supported by wxWindows are easier to handle
with PIL in which case your solution of keeping a working image in PIL
format and then converting a wxBitmap prior to display is also a good
solution. Conversion is covered in the wxPython wiki.

http://wiki.wxpython.org/

Here are some of the conversion routines PythonCard uses to deal with PIL,
wxImage, wxBitmap, and NumPy numeric arrays.

def PILToImage(pilImage):
    if (pilImage.mode != 'RGB'):
        pilImage = pilImage.convert('RGB')
    imageData = pilImage.tostring('raw', 'RGB')
    img = wx.wxEmptyImage(pilImage.size[0], pilImage.size[1])
    img.SetData(imageData)
    return img

def PILToBitmap(image):
    return wx.wxBitmapFromImage(PILToImage(image))

def BitmapToPIL(bmp):
    imageData = wx.wxImageFromBitmap(bmp).GetData()
    imagePIL = fromstring('RGB', (bmp.GetWidth(), bmp.GetHeight()),
imageData)
    imagePIL = imagePIL.convert('RGB')
    return imagePIL

def numericArrayToImage(array):
    height, width = array.shape[:2]
    img = wx.wxEmptyImage(width, height)
    img.SetData(array.tostring())
    return img

ka





More information about the Python-list mailing list