wxPython: images from URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Daugherty

    wxPython: images from URLs

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

    --

    Jonathan Daugherty
    cprogrammer.org is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, cprogrammer.org has it all. We hope you find what you are searching for!


    "It's a book about a Spanish guy called Manual, you should read it."
    -- Dilbert

  • Kevin Altis

    #2
    Re: wxPython: images from URLs

    "Jonathan Daugherty" <cygnus@cprogra mmer.org> wrote in message
    news:mailman.81 0.1075149139.12 720.python-list@python.org ...[color=blue]
    > Does anyone here know if the wxImage class in wxPython supports dislaying
    > images from URLs?[/color]

    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.wxInitAllIma geHandlers()

    # here's a real URL for testing purposes
    url = 'http://pythoncard.sour ceforge.net/images/addresses_01.pn g'

    try:
    fp = urllib.urlopen( url)
    data = fp.read()
    fp.close()
    img = wx.wxImageFromS tream(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


    Comment

    • Tim Roberts

      #3
      Re: wxPython: images from URLs

      Jonathan Daugherty <cygnus@cprogra mmer.org> wrote:[color=blue]
      >
      >Does anyone here know if the wxImage class in wxPython supports dislaying
      >images from URLs?[/color]

      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, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • Anand Pillai

        #4
        Re: wxPython: images from URLs

        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=cStringI O.StringIO(self ._imgstream)

        try:
        img=wxImageFrom Stream(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 "wxImageFromStr eam()" 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@probo.com > wrote in message news:<fgse10l8p ae12p0els55fsuj llq2irrj0l@4ax. com>...[color=blue]
        > Jonathan Daugherty <cygnus@cprogra mmer.org> wrote:[color=green]
        > >
        > >Does anyone here know if the wxImage class in wxPython supports dislaying
        > >images from URLs?[/color]
        >
        > 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.[/color]

        Comment

        • Jonathan Daugherty

          #5
          Re: wxPython: images from URLs

          # self._imgstream = urllib2.urlopen (url).read()
          # stream=cStringI O.StringIO(self ._imgstream)
          #
          # try:
          # img=wxImageFrom Stream(stream)
          # except:
          # pass

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

          try:
          bm = wxBitmap(img)
          self.bitmap.set Bitmap(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_J PEG to
          wxImageFromStre am. I have also tried omitting it as
          well.

          --

          Jonathan Daugherty
          cprogrammer.org is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, cprogrammer.org has it all. We hope you find what you are searching for!


          "It's a book about a Spanish guy called Manual, you should read it."
          -- Dilbert

          Comment

          • Anand Pillai

            #6
            Re: wxPython: images from URLs

            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 <cygnus@cprogra mmer.org> wrote in message news:<mailman.9 31.1075308709.1 2720.python-list@python.org >...[color=blue]
            > # self._imgstream = urllib2.urlopen (url).read()
            > # stream=cStringI O.StringIO(self ._imgstream)
            > #
            > # try:
            > # img=wxImageFrom Stream(stream)
            > # except:
            > # pass
            >
            > I have tried this and it appears to work, but once I have
            > the image (from wxImageFromStre am), I use it as follows:
            >
            > try:
            > bm = wxBitmap(img)
            > self.bitmap.set Bitmap(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_J PEG to
            > wxImageFromStre am. I have also tried omitting it as
            > well.[/color]

            Comment

            • Kevin Altis

              #7
              Re: wxPython: images from URLs

              "Anand Pillai" <pythonguy@Hotp op.com> wrote in message
              news:84fc4588.0 401280613.771ac 07@posting.goog le.com...[color=blue]
              > 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=cStringI O.StringIO(self ._imgstream)
              >
              > try:
              > img=wxImageFrom Stream(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 "wxImageFromStr eam()" 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@probo.com > wrote in message[/color]
              news:<fgse10l8p ae12p0els55fsuj llq2irrj0l@4ax. com>...[color=blue][color=green]
              > > Jonathan Daugherty <cygnus@cprogra mmer.org> wrote:[color=darkred]
              > > >
              > > >Does anyone here know if the wxImage class in wxPython supports[/color][/color][/color]
              dislaying[color=blue][color=green][color=darkred]
              > > >images from URLs?[/color]
              > >
              > > 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.[/color][/color]

              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.



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

              def PILToImage(pilI mage):
              if (pilImage.mode != 'RGB'):
              pilImage = pilImage.conver t('RGB')
              imageData = pilImage.tostri ng('raw', 'RGB')
              img = wx.wxEmptyImage (pilImage.size[0], pilImage.size[1])
              img.SetData(ima geData)
              return img

              def PILToBitmap(ima ge):
              return wx.wxBitmapFrom Image(PILToImag e(image))

              def BitmapToPIL(bmp ):
              imageData = wx.wxImageFromB itmap(bmp).GetD ata()
              imagePIL = fromstring('RGB ', (bmp.GetWidth() , bmp.GetHeight() ),
              imageData)
              imagePIL = imagePIL.conver t('RGB')
              return imagePIL

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

              ka


              Comment

              • Tim Roberts

                #8
                Re: wxPython: images from URLs

                Jonathan Daugherty <cygnus@cprogra mmer.org> wrote:
                [color=blue]
                ># self._imgstream = urllib2.urlopen (url).read()
                ># stream=cStringI O.StringIO(self ._imgstream)
                >#
                ># try:
                ># img=wxImageFrom Stream(stream)
                ># except:
                ># pass
                >
                >I have tried this and it appears to work, but once I have
                >the image (from wxImageFromStre am), I use it as follows:
                >
                >try:
                > bm = wxBitmap(img)
                > self.bitmap.set Bitmap(bm)
                >except Exception, e:
                > print e[/color]

                Why wouldn't you use wxBitmapFromIma ge?
                --
                - Tim Roberts, timr@probo.com
                Providenza & Boekelheide, Inc.

                Comment

                • Jonathan Daugherty

                  #9
                  Re: wxPython: images from URLs

                  # Why wouldn't you use wxBitmapFromIma ge?

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

                  stream = cStringIO.Strin gIO(imgdata)
                  imgobj = wx.wxImage(stre am)

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

                  --

                  Jonathan Daugherty
                  cprogrammer.org is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, cprogrammer.org has it all. We hope you find what you are searching for!


                  "It's a book about a Spanish guy called Manual, you should read it."
                  -- Dilbert

                  Comment

                  Working...