does anyone know how to use libjeg from within memory in python

Chris Colbert sccolbert at gmail.com
Tue Oct 13 06:12:25 EDT 2009


In answering my own question, this can be done trivially with PIL.
Here is a self contained example:


In [1]: import httplib

In [2]: from PIL import ImageFile

In [3]: import numpy as np

In [4]: conn = httplib.HTTPConnection('www.therealstevencolbert.com')

In [5]: conn.request('GET', '/dump/IMG_0408_rs.JPG')

In [6]: r1 = conn.getresponse()

In [7]: r1.status
Out[7]: 200

In [8]: data = r1.read()

In [9]: parser = ImageFile.Parser()

In [10]: parser.feed(data)

In [11]: img = parser.close()

In [12]: img.show()

In [13]: numpyimg = np.asarray(img)

In [14]: numpyimg.shape
Out[14]: (768, 1024, 3)

In [15]: numpyimg.dtype
Out[15]: dtype('uint8')



On Tue, Oct 13, 2009 at 11:51 AM, Chris Colbert <sccolbert at gmail.com> wrote:
> Say I use python to talk to a wireless webcamera that delivers images
> via http requests.
>
> I request an image and read it into a buffer, but the image is in jpeg format.
>
> I would like to convert this to a simple RGB format buffer to pass to
> numpy. Has anyone managed this using libjpeg or any other lib?
>
> Cheers!
>
> Chris
>



More information about the Python-list mailing list