[Image-SIG] Can't split data stream for an ip camera

Dom Dom mydomdom at gmail.com
Mon Sep 13 19:39:57 CEST 2010


I want to display with wxPython the images sent by an ip camera.

Getting the photos is not a problem:
page = urllib2.build_opener().open("

image = Image.open(cStringIO.StringIO(page.read()))
Then I display the images one by one with wx with a loop (4 to 8 Frames Per
Second, which is not quick enough). But it works well.

What I'd like to do is to display the frames in a continous mode, ie
The problem is that I can't manage to analyze the stream sent by the camera
whose address is: '


I tried to adapt the script mentioned here

) without success. See below.

My problem is that I don't know where to begin and where to stop the
of data (the stream of data is continous):
See the class below:
   boundary = self.buffer.find(r"""--IPCamBoundary""")
   begin = boundary + 43
   end = self.buffer.rfind(r"""--IPCamBoundary""")

I'd appreciate that some help to understand what I should do.

Many thanks in advance for four help

Below is some info that may be needed with a view to helping me:
>>> stream = urllib2.urlopen('

>>> print stream.info

Date: Sat, 01 Feb 2003 05:30:42 GMT

Server: WYM/1.0

Connection: close

Content-Type: multipart/x-mixed-replace;boundary=IPCamBoundary

Last-Modified: Sat, 01 Feb 2003 05:30:42 GMT

Pragma: no-cache

Cache-Control: no-cache

Expires: 01 Jan 1970 00:00:00 GMT

>>> stream.readline()
>>> stream.readline()
'Content-Type: image/jpeg\r\n'
>>> stream.readline()
>>> stream.readline()
>>> and so on.
I tried to attach the code returned by the camera  (with two
"--IPCamBoundary\r\n", which seem
to be the begining of a frame.) but it was rejected by the image-sig list...


class myCamera(object):
    def __init__(self, url = '

', param = None):
        self.url = url
        self.param = param
        self.state = 0
        self.buffer = ''
        self.limit = 8092

    def Start(self):
            self.fp = urllib2.build_opener().open(self.url)
        except URLError:
            print u"""Check connection"""
        self.state = True

    def Stop(self):
        self.state = 0

    def NextFrame(self):
        if self.state:
            temp = self.fp.read(self.limit)
            self.buffer += temp
            boundary = self.buffer.find(r"""--IPCamBoundary""")
            count = self.buffer.count(r"""--IPCamBoundary""")
            if boundary <> -1:
                if count > 1:
                    begin = boundary + 43
                    end = self.buffer.rfind(r"""--IPCamBoundary""")
                    buff = self.buffer[boundary:end-1]
                    output = cStringIO.StringIO()
                    contents = output.getvalue()
                    image = Image.open(cStringIO.StringIO(contents))

This script results in a traceback:
image = Image.open(cStringIO.StringIO(contents))
  File "D:\Python26\lib\site-packages\PIL\Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20100913/7d911fe8/attachment.html>

More information about the Image-SIG mailing list