wxPython: images from URLs

Discussion in 'Python' started by Jonathan Daugherty, Jan 26, 2004.

  1. Does anyone here know if the wxImage class in wxPython supports dislaying
    images from URLs?

    --

    Jonathan Daugherty
    http://www.cprogrammer.org

    "It's a book about a Spanish guy called Manual, you should read it."
    -- Dilbert
     
    Jonathan Daugherty, Jan 26, 2004
    #1
    1. Advertising

  2. Jonathan Daugherty

    Kevin Altis Guest

    "Jonathan Daugherty" <> wrote in message
    news:...
    > 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
     
    Kevin Altis, Jan 26, 2004
    #2
    1. Advertising

  3. Jonathan Daugherty

    Tim Roberts Guest

    Jonathan Daugherty <> 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.
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jan 28, 2004
    #3
  4. Jonathan Daugherty

    Anand Pillai Guest

    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 <> wrote in message news:<>...
    > Jonathan Daugherty <> 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.
     
    Anand Pillai, Jan 28, 2004
    #4
  5. # self._imgstream = urllib2.urlopen(url).read()
    # stream=cStringIO.StringIO(self._imgstream)
    #
    # try:
    # img=wxImageFromStream(stream)
    # except:
    # pass

    I have tried this and it appears to work, but once I have
    the image (from wxImageFromStream), I use it as follows:

    try:
    bm = wxBitmap(img)
    self.bitmap.setBitmap(bm)
    except Exception, e:
    print e

    And the exception (raised by wxBitmap(img)) is:

    String or Unicode type required

    (The exception is a TypeError exception.)

    Any ideas? No exceptions are raised by the block that
    creates the image from the data stream. The image is
    a JPG image, and I pass wxBITMAP_TYPE_JPEG to
    wxImageFromStream. I have also tried omitting it as
    well.

    --

    Jonathan Daugherty
    http://www.cprogrammer.org

    "It's a book about a Spanish guy called Manual, you should read it."
    -- Dilbert
     
    Jonathan Daugherty, Jan 28, 2004
    #5
  6. Jonathan Daugherty

    Anand Pillai Guest

    This is the straight forward way to do this in wxPython
    but somehow it always pops up that ugly error window.
    I remember trying many options to do this purely using
    wxPython (wxWindows), but I failed.

    If you use PIL in your program you can conver the
    wx Image instance to a PIL image of type BMP and then
    display it by reconverting it back to the wxImage
    instance. PyWiew has methods to do this. The source code
    is available somewhere in my Python page at
    http://members.lycos.co.uk/anandpillai . I no longer
    maintain that program, but the latest source code should
    be available there.

    Regards

    -Anand

    Jonathan Daugherty <> wrote in message news:<>...
    > # self._imgstream = urllib2.urlopen(url).read()
    > # stream=cStringIO.StringIO(self._imgstream)
    > #
    > # try:
    > # img=wxImageFromStream(stream)
    > # except:
    > # pass
    >
    > I have tried this and it appears to work, but once I have
    > the image (from wxImageFromStream), I use it as follows:
    >
    > try:
    > bm = wxBitmap(img)
    > self.bitmap.setBitmap(bm)
    > except Exception, e:
    > print e
    >
    > And the exception (raised by wxBitmap(img)) is:
    >
    > String or Unicode type required
    >
    > (The exception is a TypeError exception.)
    >
    > Any ideas? No exceptions are raised by the block that
    > creates the image from the data stream. The image is
    > a JPG image, and I pass wxBITMAP_TYPE_JPEG to
    > wxImageFromStream. I have also tried omitting it as
    > well.
     
    Anand Pillai, Jan 29, 2004
    #6
  7. Jonathan Daugherty

    Kevin Altis Guest

    "Anand Pillai" <> wrote in message
    news:...
    > 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 <> wrote in message

    news:<>...
    > > Jonathan Daugherty <> 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
     
    Kevin Altis, Jan 29, 2004
    #7
  8. Jonathan Daugherty

    Tim Roberts Guest

    Jonathan Daugherty <> wrote:

    ># self._imgstream = urllib2.urlopen(url).read()
    ># stream=cStringIO.StringIO(self._imgstream)
    >#
    ># try:
    ># img=wxImageFromStream(stream)
    ># except:
    ># pass
    >
    >I have tried this and it appears to work, but once I have
    >the image (from wxImageFromStream), I use it as follows:
    >
    >try:
    > bm = wxBitmap(img)
    > self.bitmap.setBitmap(bm)
    >except Exception, e:
    > print e


    Why wouldn't you use wxBitmapFromImage?
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jan 30, 2004
    #8
  9. # Why wouldn't you use wxBitmapFromImage?

    The api docs suggest that using the wxBitmap(image) constructor
    is better. Besides: this is beside the point -- this code fails:

    stream = cStringIO.StringIO(imgdata)
    imgobj = wx.wxImage(stream)

    The second line raises a TypeError: "String or Unicode type
    required."

    --

    Jonathan Daugherty
    http://www.cprogrammer.org

    "It's a book about a Spanish guy called Manual, you should read it."
    -- Dilbert
     
    Jonathan Daugherty, Jan 30, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    476
    Andrew Thompson
    Jan 4, 2004
  2. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    750
    Sriram Srivatsan
    Aug 12, 2008
  3. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,228
    Adam Monsen
    Feb 8, 2009
  4. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    293
    Steve T.
    Mar 4, 2004
  5. Simon Wigzell

    Automatically save images from image URLs

    Simon Wigzell, Dec 11, 2007, in forum: ASP General
    Replies:
    1
    Views:
    96
    Evertjan.
    Dec 11, 2007
Loading...

Share This Page