how can I convert an image into a matrix?

Edward C. Jones edcjones at erols.com
Sun May 27 01:16:53 EDT 2001


akhar wrote:

> import Image, Numeric
>
> def ImageToArray(i):
>     a = Numeric.array(i.tostring(), "b")
>     a.shape = i.size[1], i.size[0]
>     return a
>
> im = Image.open('c:/testimg.jpg', 'r')
> im.show()
> matrix = ImageToArray(im)

The image is a jpg which has three color bands. Use im.split() to separate the
bands and convert to arrays separately. Or convert to a three dimensional array
of shape (i.size[1], i.size[0], 3).

See my web page at: http://members.tripod.com/~edcjones/utility02.html

Ed Jones





More information about the Python-list mailing list