[Numpy-discussion] Interfacing with PIL?
tim.hochberg at ieee.org
Wed Aug 30 13:16:58 CDT 2006
Johannes Loehnert wrote:
> 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.
> 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.
On a related note, does anyone have a good recipe for converting a PIL
image to a wxPython image? The last time I tried this, the best I could
come up with was:
stream = cStringIO.StringIO()
img.save(stream, "png") # img is PIL Image
image = wx.ImageFromStream(stream) # image is a wxPython Image
More information about the Numpy-discussion