[Numpy-discussion] Interfacing with PIL?

Johannes Loehnert a.u.r.e.l.i.a.n at gmx.net
Wed Aug 30 14:10:59 EDT 2006


Am Mittwoch, 30. August 2006 19:20 schrieb Ghalib Suleiman:
> I'm somewhat new to both libraries...is there any way to create a 2D
> array of pixel values from an image object from the Python Image
> Library? I'd like to do some arithmetic on the values.

Yes.

To transport the data:
>>> import numpy
>>> image = <some PIL image>
>>> arr = numpy.fromstring(image.tostring(), dtype=numpy.uint8)

(alternately use dtype=numpy.uint32 if you want RGBA packed in one number).

arr will be a 1d array with length (height * width * b(ytes)pp). Use reshape 
to get it into a reasonable form.

HTH, 
Johannes




More information about the NumPy-Discussion mailing list