[Image-SIG] Quicker image transfer, tobuffer?

kevin at cazabon.com kevin at cazabon.com
Tue Jul 11 22:10:29 CEST 2006


Although I'm not really up to speed on the array interface, accessing the 
pixel data in a PIL image isn't really that difficult in C/C++... the only 
challenge I would see (besides tracking the channels/padding correctly... 
trivial) would be getting the pointer into Python to pass it to NumPy.  I've 
written a few modules in C that directly modify the PIL buffer data, with 
simple code such as attached (lines 186-214 show it clearly).

(This is a module that does unsharp-masking and gaussian blur on PIL 
images... Fredrik is welcome to include this directly into the PIL library 
if he sees fit, for which I'll gladly remove ANY licensing restrictions)

Kevin.


----- Original Message ----- 
From: "Travis Oliphant" <oliphant.travis at ieee.org>
To: <image-sig at python.org>
Cc: "numpy-discussion" <numpy-discussion at lists.sourceforge.net>
Sent: Tuesday, July 11, 2006 9:37 PM
Subject: Re: [Image-SIG] Quicker image transfer, tobuffer?


>
> Here is a simple approach to allowing the PIL to export the array
> interface.
>
> This allows NumPy to create a suitable array from a PIL image very easily:
>
>
> At the top of Image.py add the following
>
> if sys.byteorder == 'little':
>     _ENDIAN = '<'
> else:
>     _ENDIAN = '>'
>
> _MODE_CONV = {
>
>     # official modes
>     "1": ('|b1', None),
>     "L": ('|u1', None),
>     "I": ('%si4' % _ENDIAN, None),
>     "F": ('%sf4' % _ENDIAN, None),
>     "P": ('|u1', None),
>     "RGB": ('|u1', 3),
>     "RGBX": ('|u1', 4),
>     "RGBA": ('|u1', 4),
>     "CMYK": ('|u1', 4),
>     "YCbCr": ('|u1', 4),
>
>     # Experimental modes include I;16, I;16B, RGBa, BGR;15,
>     # and BGR;24.  Use these modes only if you know exactly
>     # what you're doing...
>
> }
>
> def _conv_type_shape(im):
>     shape = im.size
>     typ, extra = _MODE_CONV[im.mode]
>     if extra is None:
>         return shape, typ
>     shape += (extra,)
>     return shape, typ
>
>
>
> In the Image class structure add
>
>     def __get_array_interface__(self):
>         new = {}
>         shape, typestr = _conv_type_shape(self)
>         new['shape'] = shape
>         new['typestr'] = typestr
>         new['data'] = self.tostring()
>         return new
>
>     __array_interface__ = property(__get_array_interface__, None,
> doc="array interface")
>
>
>
> With this addition you can then do
>
> import Image, numpy
>
> im = Image.open('lena.jpg')
> a = numpy.asarray(im)
>
> and you will get a suitable read-only array pointing to the string
> produced by tostring.
>
>
> This would be a nice thing to add to the PIL.
>
> -Travis Oliphant
>
>
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
> 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: PILusm.cpp
Url: http://mail.python.org/pipermail/image-sig/attachments/20060711/7ee6bd58/attachment-0001.pot 


More information about the Image-SIG mailing list