how can I convert an image into a matrix?

akhar akhar at videotron.ca
Sun May 27 01:00:01 EDT 2001


With these modifications I now get :
"Traceback (most recent call last):
  File "C:\Python20\Pythonwin\pywin\framework\scriptutils.py", line 301, in
RunScript
    exec codeObject in __main__.__dict__
  File "C:\My Documents\view.py", line 10, in ?
    matrix = ImageToArray(im)
  File "C:\My Documents\view.py", line 5, in ImageToArray
    a.shape = i.size[0], i.size[1]
ValueError: total size of new array must be unchanged"

---
new script
---
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)
matrix


akhar <akhar at videotron.ca> a écrit dans le message :
H0ZP6.4753$s75.999009 at weber.videotron.net...
> how can I convert an image into a matrix with the associated pixel colour
> value ? I tried playing around with PIL and Numeric but I don't get
anything
> but error messages I tried this:
>
> Import Image, ImageChops, Numeric
> im = Image.open("c:/testimg.jpg)
> def ImageToArray(i):
>     a = Numeric.array(i._tostring(), "b")
>     a.shape = i.size[1], i.size[0]
>     return a
> matrix = ImageToArray(im)
>
>   all I get is this:
>
> "Traceback (innermost last):
>   File "<interactive input>", line 1, in ?
>   File "<interactive input>", line 2, in imgtoarray
> AttributeError: 'JpegImageFile' instance has no attribute '_tostring'
> >>> "
> And while I am at it how do I use PIL to write images from a stream?
>
> Regards
>
> Stephane
>
>





More information about the Python-list mailing list